在 Pandas 中将相同键的字典加入数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25813529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:28:08 来源:igfitidea点击:
Joining same-key dictionaries into a dataframe in pandas
提问by Anton Tarasenko
How to create a pandas DataFrameout of two and more dictionaries having common keys? That is, to convert
如何DataFrame从两个或更多具有公共键的字典中创建一个Pandas?也就是说,要转换
d1 = {'a': 1}
d2 = {'a': 3}
...
into a dataframe with columns ['d1', 'd2', ...], rows indexed like "a"and values determined by the respective dictionaries?
到一个数据帧中,列['d1', 'd2', ...],行索引,"a"以及由各自字典确定的值?
采纳答案by unutbu
import pandas as pd
d1 = {'a': 1, 'b':2}
d2 = {'a': 3, 'b':5}
df = pd.DataFrame([d1, d2]).T
df.columns = ['d{}'.format(i) for i, col in enumerate(df, 1)]
yields
产量
In [40]: df
Out[40]:
d1 d2
a 1 3
b 2 5

