简体   繁体   中英

How do I turn Dictionary items into multiple DataFrames in Python?

I want to create a multiple DataFrames for each item in a dictionary. So if my dictionary was this for example:

dict = {'a' : 100, 'b' : 200, 'c' : 300}

I would like the output to be the same as create each df individually like:

pd.DataFrame(data=['100'],columns=['a'])
pd.DataFrame(data=['200'],columns=['b'])
pd.DataFrame(data=['300'],columns=['c'])

Giving me 3 deperate DataFrame, with each one being named by the dictionary key.

I've tried using a for loop, to iterate over each key however I can't manage to store the value in a DataFrame.

Thanks!

Use list comprehension for list of DataFrame s:

d = {'a' : 100, 'b' : 200, 'c' : 300}

dfs = [pd.DataFrame({k:[v]}) for k, v in d.items()]
print (dfs)
[     a
0  100,      b
0  200,      c
0  300]

print (dfs[0])
     a
0  100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM