[英]How to create a Dataframe from Dictionary where the key and values are all columns?
Example of dictionary: 字典示例:
{'892420643555336193': (8374, 38224),
'892177421306343426': (6181, 32765),
'891815181378084864': (4091, 24675),
'891689557279858688': (8512, 41548),
'891327558926688256': (9219, 39740)}
I want a dataframe where (example of first row) 我想要一个数据框(第一行的示例)
Column A
892420643555336193
Column B
8374
Column C
38224
You can create a dataframe of your dictionary keys, and one of your dictionary values, and use pd.concat
to combine them: 您可以创建一个包含字典键和一个字典值的数据pd.concat
,然后使用pd.concat
进行组合:
import pandas as pd
# where d is your dictionary
df = pd.concat((pd.DataFrame(list(d.keys())),pd.DataFrame(list(d.values()))),axis=1)
>>> df
0 0 1
0 892420643555336193 8374 38224
1 892177421306343426 6181 32765
2 891815181378084864 4091 24675
3 891689557279858688 8512 41548
4 891327558926688256 9219 39740
Alternatively, create a normal dataframe with your dictionary, transpose it, and reset the index (note the ordering is switched): 或者,用您的字典创建一个普通的数据框,将其转置并重置索引(请注意顺序已切换):
df = pd.DataFrame(d).T.reset_index()
>>> df
index 0 1
0 891327558926688256 9219 39740
1 891689557279858688 8512 41548
2 891815181378084864 4091 24675
3 892177421306343426 6181 32765
4 892420643555336193 8374 38224
You can then rename your columns as you wish: 然后,您可以根据需要重命名列:
df.columns = ['A','B','C']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.