pandas 如何将字典作为一行添加到DataFrame?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42632470/
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-14 03:07:47  来源:igfitidea点击:

How to add dictionaries to a DataFrame as a row?

pythonpandasdictionarydataframe

提问by Sinchetru

I have a DataFramewith following columns:

我有一个DataFrame以下列:

columns = ['Autor', 'Pre?ul', 'Suprafa?a total?', 'Etaj', 'Etaje', 'Tipul casei', 'Tipul de camere','Num?rul de camere','Starea apartamentului', 'Planificare', 'Tipul cl?dirii', 'Sectorul', 'Strada',  'Num?rul casei']
df = pd.DataFrame(columns=columns)

I want to add to this DataFramea number of dictionaries row by row, for instance for the first row I want to ad this dictionary:

我想DataFrame逐行添加一些字典,例如,对于第一行,我想添加这本字典:

{'Autor': nan,
 'Balcon/loj?': '2',
 'Etaj': '1',
 'Grup sanitar': 'separat',
 'Locul de amplasare ?n cas?': 'In mijlocul casei',
 'Num?rul casei': nan,
 'Num?rul de camere': '4 ?i mai multe camere',
 'Parcare': 'deschis?',
 'Pre?ul': nan,
 'Sectorul': nan,
 'Strada': nan,
 'Suprafa?a total?': '90 m2',
 'Tipul cl?dirii': 'Dat ?n exploatare'}

The values of the keys of the dictionary that are not in the DataFramecolumns should be set as NaNvalues. The dictionaries had only a part of the columns names as keys.

不在DataFrame列中的字典键的NaN值应设置为值。字典只有一部分列名作为键。

for instance the second dict:

例如第二个字典:

{'Autor': nan,
 'Num?rul casei': nan,
 'Num?rul de camere': '3 camere',
 'Pre?ul': nan,
 'Sectorul': nan,
 'Strada': nan,
 'Suprafa?a total?': '103 m2',
 'Tipul cl?dirii': 'Dat ?n exploatare'}

The dictionaries are results of a for loop and they should be added as unique row.

字典是 for 循环的结果,它们应该作为唯一行添加。

采纳答案by juanpa.arrivillaga

Use the pandas.DataFrame.from_dictalternative constructor. Build your "rows" into a list to begin with:

使用pandas.DataFrame.from_dict替代构造函数。将您的“行”构建到一个列表中,以开始:

In [22]: import numpy as np

In [23]: nan = np.nan

In [24]: rows = []

In [25]: rows.append({'Autor': nan,
    ...:  'Balcon/loj?': '2',
    ...:  'Etaj': '1',
    ...:  'Grup sanitar': 'separat',
    ...:  'Locul de amplasare ?n cas?': 'In mijlocul casei',
    ...:  'Num?rul casei': nan,
    ...:  'Num?rul de camere': '4 ?i mai multe camere',
    ...:  'Parcare': 'deschis?',
    ...:  'Pre?ul': nan,
    ...:  'Sectorul': nan,
    ...:  'Strada': nan,
    ...:  'Suprafa?a total?': '90 m2',
    ...:  'Tipul cl?dirii': 'Dat ?n exploatare'})

In [26]: rows.append({'Autor': nan,
    ...:  'Num?rul casei': nan,
    ...:  'Num?rul de camere': '3 camere',
    ...:  'Pre?ul': nan,
    ...:  'Sectorul': nan,
    ...:  'Strada': nan,
    ...:  'Suprafa?a total?': '103 m2',
    ...:  'Tipul cl?dirii': 'Dat ?n exploatare'})

Then, just make sure the pass the appropriate "orient" argument:

然后,只需确保传递适当的“orient”参数:

In [28]: pd.DataFrame.from_dict(rows, orient='columns')
Out[28]:
   Autor Balcon/loj? Etaj Grup sanitar Locul de amplasare ?n cas?  \
0    NaN           2    1      separat          In mijlocul casei
1    NaN         NaN  NaN          NaN                        NaN

   Num?rul casei      Num?rul de camere   Parcare  Pre?ul  Sectorul  Strada  \
0            NaN  4 ?i mai multe camere  deschis?     NaN       NaN     NaN
1            NaN               3 camere       NaN     NaN       NaN     NaN

  Suprafa?a total?     Tipul cl?dirii
0            90 m2  Dat ?n exploatare
1           103 m2  Dat ?n exploatare

EDIT

编辑

Actually, just noticed the normal constructor works just fine, and doesn't need any arguments!

实际上,只是注意到普通构造函数工作得很好,并且不需要任何参数!

In [31]: pd.DataFrame(rows)
Out[31]:
   Autor Balcon/loj? Etaj Grup sanitar Locul de amplasare ?n cas?  \
0    NaN           2    1      separat          In mijlocul casei
1    NaN         NaN  NaN          NaN                        NaN

   Num?rul casei      Num?rul de camere   Parcare  Pre?ul  Sectorul  Strada  \
0            NaN  4 ?i mai multe camere  deschis?     NaN       NaN     NaN
1            NaN               3 camere       NaN     NaN       NaN     NaN

  Suprafa?a total?     Tipul cl?dirii
0            90 m2  Dat ?n exploatare
1           103 m2  Dat ?n exploatare

回答by Michael

You can loop over the dictionaries, append the results for each dictionary to a list, and then add the list as a row in the DataFrame.

您可以遍历字典,将每个字典的结果附加到一个列表中,然后将该列表作为一行添加到 DataFrame 中。

dflist = []
for dic in dictionarylist:
   rlist = []
   for key in keylist:
       if dic[key] is None:
           rlist.append(None)
       else:
           rlist.append(dic[key])

   dflist.append(rlist)

df = pd.DataFrame(dflist)