Home > Back-end >  Pandas dataframe - column with list of dictionaries, extract values and convert to comma separated v
Pandas dataframe - column with list of dictionaries, extract values and convert to comma separated v

Time:12-22

I have the following dataframe that I want to extract each numerical value from the list of dictionaries and keep in the same column. for instance for the first row I would want to see in the data column: 179386782, 18017252, 123452

id data
12345 [{'id': '179386782'}, {'id': 18017252}, {'id': 123452}]

below is my code to create the dataframe above ( I've hardcoded stories_data as an example)

for business_account in data:
    business_account_id = business_account[0]
    stories_data = {'data': [{'id': '179386782'}, {'id': '18017252'}, {'id': '123452'}]}
    df = pd.DataFrame(stories_data.items())
    df.set_index(0, inplace=True)
    df = df.transpose()
    df_stories['id'] = business_account_id
    col = df_stories.pop("id")
    df_stories.insert(0, col.name, col)

I've tried this: df_stories["data"].str[0]

but this only returns the first element (dictionary) in the list

CodePudding user response:

Try:

df['data'] = df['data'].apply(lambda x: ', '.join([str(d['id']) for d in x]))
print(df)

# Output:
      id                         data
0  12345  179386782, 18017252, 123452

Another way:

df['data'] = df['data'].explode().str['id'].astype(str) \
                       .groupby(level=0).agg(', '.join)
print(df)

# Output:
      id                         data
0  12345  179386782, 18017252, 123452
  • Related