serhii.net

In the middle of the desert you can say anything you want

11 May 2023

Pandas add colums to dataframe based on dictionary inside a column

Courtesy of ChatGPT, after googling for 15 minutes didn’t bring anything immediately copy-pasteable.

I’ll deal with the existential crisis from this later.

To explode the cells containing dictionaries in a pandas DataFrame and append them as additional columns, you can use the apply() method along with the pd.Series() constructor. Here’s an example:

import pandas as pd

# create example DataFrame
df = pd.DataFrame({
    'id': [1, 2],
    'info': [
        {'name': 'Alice', 'age': 25},
        {'name': 'Bob', 'age': 30, 'city': 'New York'}
    ]
})

# use apply() with pd.Series() to explode the dictionaries and append them as columns
df = pd.concat([df.drop('info', axis=1), df['info'].apply(pd.Series)], axis=1)

print(df)

Output:

   id   name  age      city
0   1  Alice   25       NaN
1   2    Bob   30  New York

In this example, we first create a DataFrame with two rows, where the info column contains dictionaries. We then use the apply() method with pd.Series() to explode the dictionaries and create additional columns for each key in the dictionaries. Finally, we concatenate the exploded columns with the original DataFrame using pd.concat().

Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus