0

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)
2
  • What's the problem with your solution? I suspect you're missing an iteritems for the loop to work. Commented Feb 10, 2017 at 15:21
  • What are the keys in the dictionaries? How should they relate to columns in the new dataframe? Commented Feb 10, 2017 at 16:16

3 Answers 3

1
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}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.