问题描述
我得到了一个 Pandas 数据框,其中一列的值如下所示:
>>> df['video_p25_watched_actions']
[{'action_type': 'video_view','value': '137520'}]
Index | video_p25_watched_actions | p25
-----------------------------------------------------------------
0 | [{'action_type': 'video_view','value': '137520'}] | 137520
我用一些原始数据创建了一个谷歌表来展示它的样子:
https://docs.google.com/spreadsheets/d/1aJDiXFyUIb9gZCA1-pPDxciPQWv0vcCairY-pkdGg_A/edit?usp=sharing
先谢谢你!
解决方法
由于列中的所有行具有相同的结构,因此您可以使用此
df['new_column'] = df['video_p25_watched_actions'].apply(lambda x: ''.join(e for e in x.split(":")[2] if e.isalnum()))
,
试试:
df['value']= df['video_p25_watched_actions'].replace(regex=True,to_replace='[^0-9]',value=' ')
仅从 df['video_p25_watched_actions'] 中获取值,其他字母将被空格替换