[英]Extract data from a pandas series if the values are in a dictionary-like format
I try the solution in Extracting dictionary values from a pandas dataframe But it didn't work. 我尝试了从pandas数据框中提取字典值的解决方案,但是没有用。
I have a pandas.core.series.Series with the following general format: 我有一个pandas.core.series.Series,具有以下常规格式:
0 {'hashtags': [], 'symbols': [], 'user_mentions...
1 {'hashtags': [], 'symbols': [], 'user_mentions...
2 {'hashtags': [], 'symbols': [], 'user_mentions...
3 {'hashtags': [], 'symbols': [], 'user_mentions...
...
the specific format of each one is similar to the following: 每种格式的具体格式类似于以下内容:
{'hashtags': [],
'symbols': [],
'user_mentions': [{'screen_name': 'jose_m',
'id_str': '132',
'name': 'Jose',
'indices': [0, 10],
'id': 103},
{'screen_name': 'paul',
'id_str': '243403',
'name': 'Jorge',
'indices': [50, 64],
'id': 2423}],
'urls': []}
I get that by placing the index zero to the variable entities[0]
(Index may change). 我通过将索引零放置到变量
entities[0]
(索引可能会更改)来实现。
I need to extract extract all the screen_name and name inside user_mentions. 我需要解压缩user_mentions中的所有screen_name和名称 。 Thanks :)
谢谢 :)
Here is an example with apply , for each entities
returns a list with a tuple for each user_mention
: 这是apply的示例,每个
entities
为每个user_mention
返回一个带有元组的列表:
def find_user_mention(user_mention):
return (user_mention['screen_name'], user_mention['name'])
df['entities'].apply(lambda x: [find_user_mention(user_mention) for user_mention in x['user_mentions']])
Example output with random data: 输出带有随机数据的示例:
0 [(NunkMasKKs, 🍣 SUSHIPLANERO 🍣)]
1 [(leobilanski, Leo Bilanski)]
2 [(romerodiario, El Profe Romero)]
3 [(HugoYasky, Hugo Yasky)]
4 [(marianorecalde, Mariano Recalde)]
5 [(cyngarciaradio, Cynthia García)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.