Reputation: 291
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)
Upvotes: 0
Views: 1787
Reputation: 1282
I don't know what your dict in dataframe.column
looks like. If it looks like the dictionary below, I think you can use pandas.concat
to concentrate dictionaries together.
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)
Upvotes: 0
Reputation: 624
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}
Upvotes: 1
Reputation: 11391
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.
Upvotes: 0