简体   繁体   中英

How to extract value out of column with list of dictionary using pandas dataframe

I have this dataframe

| date | value
|------|------|
|2020-08-12| [{'action_type': 'video_view', 'value': '527'}]|
|2020-08-12|[{'action_type': 'video_view', 'value': '662'}]|
|2020-07-01| [{'action_type': 'video_view', 'value': '69'}]|

My desired output should be

| date | value
|------|------|
|2020-08-12|527|
|2020-08-12|662|
|2020-07-01|69|

PLEASE HELP

Given the value column contains array data, you can extract it as follows:

df['value'] = df['value'].apply(lambda data: data[0].get('value'))

You can do this easily by using str accessor:

df['value'] = df['value'].str[0].str['value']

And if you need to do some mathematics and calculation by value column, don't forget to also change the type to numeric like below:

df['value'] = df['value'].astype(int)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM