abhi
abhi

Reputation: 377

How can I create column from dictionary keys in same dataframe?

I have a dataframe, something like:

|   | a | b                |
|---|---|------------------|
| 0 | a | {'d': 1, 'e': 2} |
| 1 | b | {'d': 3, 'e': 4} |
| 2 | c | NaN              |
| 3 | d | {'f': 5}         |

How can make something like this:

|   | a | b                | d | e | f |
|---|---|------------------|---|---|---|
| 0 | a | {'d': 1, 'e': 2} | 1 | 2 |nan|
| 1 | b | {'d': 3, 'e': 4} | 3 | 4 |nan|
| 2 | c | NaN              |nan|nan|nan|
| 3 | d | {'f': 5}         |nan|nan| 5 |


I tried doing this Split / Explode a column of dictionaries into separate columns with pandas but due to null values present, it is throwing an error.
'float' object has no attribute 'items'

Upvotes: 0

Views: 913

Answers (2)

mujjiga
mujjiga

Reputation: 16866

Replace NaN with None and then proceed

df = pd.DataFrame({'a':['a','b','c','d'], 
                   'b':[{'d': 1, 'e': 2},
                        {'d': 3, 'e': 4},
                        np.nan,
                        {'f': 5}]
                   })

df = df.where(pd.notnull(df), None)
pd.concat([df, df['b'].apply(pd.Series)], axis=1)

Output:

   a                 b    d    e    f
0  a  {'d': 1, 'e': 2}  1.0  2.0  NaN
1  b  {'d': 3, 'e': 4}  3.0  4.0  NaN
2  c              None  NaN  NaN  NaN
3  d          {'f': 5}  NaN  NaN  5.0

Upvotes: 0

Sayandip Dutta
Sayandip Dutta

Reputation: 15872

You can try the following:

>>> df
   a                 b
0  a  {'d': 1, 'e': 2}
1  b  {'d': 3, 'e': 4}
2  c               NaN
3  d          {'f': 5}

>>> df.join(pd.DataFrame.from_records(df['b'].mask(df.b.isna(), {}).tolist()))

   a                 b    d    e    f
0  a  {'d': 1, 'e': 2}  1.0  2.0  NaN
1  b  {'d': 3, 'e': 4}  3.0  4.0  NaN
2  c               NaN  NaN  NaN  NaN
3  d          {'f': 5}  NaN  NaN  5.0

Upvotes: 1

Related Questions