简体   繁体   English

如果值采用类似字典的格式,则从熊猫系列中提取数据

[英]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.

相关问题 如何从熊猫中杂乱无章的字典状数据框中提取分数? - How to extract a score from a disorganised dictionary-like data frame in pandas? 将包含重复变量名称和变量值的类字典对象列表转换为 Pandas dataframe - Converting a list of dictionary-like objects containing repeated variable names and variable values to a Pandas dataframe 高效的数据结构可让熊猫般的字典式观察按时间在关键时刻锁定? - Efficient data structure to keep dictionary-like observations keyed by time in Pandas? 将嵌套的类似字典的 txt 文件读入 Pandas 数据帧 - Reading nested dictionary-like txt file into a Pandas dataframe pandas DataFrame 行的类字典 get() 方法? - dictionary-like get() method for rows of a pandas DataFrame? 如何在类似字典的字符串的列中获取值? - How to get values in a column of dictionary-like string? Django:存储/查询类似字典的数据集? - Django: storing/querying a dictionary-like data set? Python(或一般的CS)中的循环或分层字典式数据结构? - A cyclical or hierarchical dictionary-like data structure in Python (or in CS in general)? 使用PyiCloud解析类似于字典的Apple日历数据 - Parsing dictionary-like Apple calendar data with PyiCloud 从作为字典中值的熊猫系列中提取索引 - extracting the indices from pandas series that are values in the dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM