简体   繁体   English

在数据框中循环遍历Pandas字典

[英]Looping through Pandas dict within dataframe

I have a dataframe with a column who's rows each contain a dict. 我有一个带有一列的数据框,每行各包含一个字典。

I would like to extract those dict's and turn them into dataframes so I can merge them together. 我想提取那些字典并将其转换为数据框,以便将它们合并在一起。

What's the best way to do this? 最好的方法是什么?

Something like: 就像是:

for row in dataframe.column:
    dataframe_loop = pd.DataFrame(dataframe['column'].iloc(row), columns=['A','B'])
    dataframe_result = dataframe_result.append(dataframe_loop)
import pandas as pd

d = {'col': pd.Series([{'a':1}, {'b':2}, {'c':3}])}

df = pd.DataFrame(d)

>>>print(df)

      col 
 0  {'a': 1} 
 1  {'b': 2} 
 2  {'c': 3}

res = {}

for row in df.iterrows():
    res.update(row[1]['col'])

>>>print(res) 
{'b': 2, 'a': 1, 'c': 3}

If your column contains dicts and you want to make a dataframe out of those dicts, you can just convert the column to a list of dicts and make that into a dataframe directly: 如果您的列包含字典,并且您想从这些字典中创建一个数据框,则只需将列转换为字典列表,然后直接将其制成数据框即可:

pd.DataFrame(dataframe['column'].tolist())

The dictionary keys will become columns. 字典键将成为列。 If you want other behavior, you'll need to specify that. 如果您想要其他行为,则需要指定。

I don't know what your dict in dataframe.column looks like. 我不知道您在dataframe.column的字典是什么样的。 If it looks like the dictionary below, I think you can use pandas.concat to concentrate dictionaries together. 如果看起来像下面的字典,我认为您可以使用pandas.concat将字典集中在一起。

import pandas as pd

# create a dummy dataframe
dataframe = pd.DataFrame({'column':[{'A':[1,2,3], 'B':[4,5,6]}, \
                                    {'A':[7,8,9], 'B':[10,11,12]}, \
                                    {'A':[13,14,15], 'B':[16,17,18]}]})

#print(dataframe)

res = pd.concat([pd.DataFrame(row, columns=['A', 'B']) for row in dataframe.column], ignore_index=True)

print(res)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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