nanounanue
nanounanue

Reputation: 8342

How to split a pandas column of type dict in columns?

I have a pandas dataframe. One of the columns of the dataframe is a dict object. The following dataframe is a toy example of the real dataframe:

 DF = pd.DataFrame({'id':[1,2,3], 'col1':[{'a':1, 'b':2, 'c':3}, {'a':3, 'b':4, 'c':5}, {'a':None, 'b':5, 'c':6}]})

I would like to split the col1 in columns: one column per dictionary key. All the rows have the same keys.

After the splitting the dataframe should look like:

id  a   b    c
1    1  2    3
2   3   4    5
3  None  5   6

NOTE: I got the dict column from a jsonb column in postgresql.

Upvotes: 1

Views: 1119

Answers (3)

cosmic_inquiry
cosmic_inquiry

Reputation: 2684

Input:

df = pd.DataFrame({'id':[1,2,3], 'col1':[{'a':1, 'b':2, 'c':3}, {'a':3, 'b':4, 'c':5}, {'a':None, 'b':5, 'c':6}]})
df.set_index('id').col1.apply(pd.Series)

Output:

      a    b    c
id
1   1.0  2.0  3.0
2   3.0  4.0  5.0
3   NaN  5.0  6.0

Upvotes: 3

U13-Forward
U13-Forward

Reputation: 71570

Try:

df=pd.DataFrame(DF['col1'].tolist())
df['id']=DF['id']

Then now:

print(df)

IS:

     a  b  c  id
0  1.0  2  3   1
1  3.0  4  5   2
2  NaN  5  6   3

Do:

df=pd.DataFrame(DF['col1'].tolist())
df.insert(0,'id',DF['id'])
print(df)

For putting 'id' at front

Output:

   id    a  b  c
0   1  1.0  2  3
1   2  3.0  4  5
2   3  NaN  5  6

Upvotes: 2

Sociopath
Sociopath

Reputation: 13401

I think you need:

df = pd.concat([DF.drop(['col1'], axis=1), DF['col1'].apply(pd.Series)], axis=1)

output

    id  a     b     c
0   1   1.0   2.0   3.0
1   2   3.0   4.0   5.0
2   3   NaN   5.0   6.0

Upvotes: 1

Related Questions