Home > Enterprise >  Separate nested dict inside a series column of a dataframe
Separate nested dict inside a series column of a dataframe

Time:12-12

dataframe

Trying to extract the username from the author column of the dataframe for each row, the author column is series & the individual values inside author is a dictionary.

Converting the author column directly to df & & changing the type of author column not helping to reach the goal.

I'm only able to reference the username via

df_item['author'][0]['username']

Trying to get a separate username column

id                  type   content        channel_id           username
1047404831062638613 0      It’s really..   1047331843898359849  lips
1047333443165499432 0      okay,thankyou   1047331843898359849   Mj

CodePudding user response:

Did you try this:

df['author_username'] = df['author'].apply(lambda x: x['username'])

CodePudding user response:

First convert the series to list then pass it to pandas dataframe and it takes care of the rest.

df_new = DataFrame(list(df2["author"]))
  • Related