pythonpandasdataframefacebook-insights

Python, Pandas DF. Take number from a string and add it to a new column


I got a pandas dataframe where one of the columns have values that look like this:

>>> df['video_p25_watched_actions']
[{'action_type': 'video_view', 'value': '137520'}]

I would like to extract the value number, and add it to a new column, so expected result would be:

Index |           video_p25_watched_actions                | p25
-----------------------------------------------------------------
0     | [{'action_type': 'video_view', 'value': '137520'}] | 137520

I've created a google sheet with some raw data to show how it wanted it to look like:

https://docs.google.com/spreadsheets/d/1aJDiXFyUIb9gZCA1-pPDxciPQWv0vcCairY-pkdGg_A/edit?usp=sharing

Thank you in advance!


Solution

  • Try:

    df['value']= df['video_p25_watched_actions'].replace(regex=True,to_replace='[^0-9]',value=' ')
    

    to get only the value from df['video_p25_watched_actions'] and other letters would be replaced by space