Pandas - Add Columns to a DataFrame Based in Dict from one of the Columns

Viewed 4706

I have the pandas.DataFrame below:

enter image description here

One of the columns from the Dataframe, pontos, holds a dict for each of the rows.

What I want to do is add one column to the DataFrame for each key from this dict. So the new columns would be, in this example: rodada, mes, etc, and for each row, these columns would be populated with the respective value from the dict.

So far I've tried the following for one of the keys:

df_times["rodada"] = [df_times["pontos"].get('rodada') for d in df_times["pontos"]]

However, as a result I'm getting a new column rodada filled with None values:

enter image description here

Any hints on what I'm doing wrong?

3 Answers

you can also use join and pandas apply function:

df=df.join(df['pontos'].apply(pd.Series))
Related