【问题标题】:How to get the text elements from python dictionaries如何从python字典中获取文本元素
【发布时间】:2021-12-29 01:48:34
【问题描述】:

我正在尝试从 pandas 专栏中获取一些内容。熊猫数据框是 df 并且有一个名为实体的列。此栏似乎包含字典。其中之一是“hashtags”,其中包含另一个字典“text”。单行中可以有多个“文本”元素。想要获取文本字段的值。下面的例子可以帮助理解。 如果我能获得有关获取主题标签值的指导,将会很有帮助。

df['entities']

输出 =>

0  {'hashtags': [], 'symbols': [], 'user_mentions': [], 'urls': [{'url': '', 'expanded_url': 'https://twitter.com/i/web/status/1460865425911205891', 'display_url': 'twitter.com/i/web/status/1…', 'indices': [117, 140]}]}
1  {'hashtags': [{'text': 'carbon', 'indices': [17, 24]}, {'text': 'nature', 'indices': [48, 55]}], 'symbols': [], 'user_mentions': [], 'urls': [{'url': '', 'expanded_url': 'https://rpujolviven.blogspot.com/2012/10/infinite-growth.html', 'display_url': 'rpujolviven.blogspot.com/2012/10/infini…', 'indices': [61, 84]}, {'url': '', 'expanded_url': 'https...
2  {'hashtags': [{'text': 'Lincoln', 'indices': [69, 77]}], 'symbols': [], 'user_mentions': [], 'urls': [{'url': '', 'expanded_url': 'https://thelincolnite.co.uk/2021/11/professor-duncan-french-the-juggernaut-of-climate-politics-rolls-on/', 'display_url': 'thelincolnite.co.uk/2021/11/profes…', 'indices': [78, 101]}]}
3  {'hashtags': [{'text': 'CBDC', 'indices': [107, 112]}], 'symbols': [], 'user_mentions': [], 'urls': [{'url': , 'expanded_url': 'https://twitter.com/i/web/status/1460865388699344900', 'display_url': 'twitter.com/i/web/status/1…', 'indices': [115, 138]}]}

预期输出:

 0  []
 1  ['carbon', 'nature']
 2  ['Lincoln']
 3  ['CBDC']

【问题讨论】:

    标签: python python-3.x dataframe dictionary tweepy


    【解决方案1】:

    试试df['entities'].map(lambda x:[i['text'] for i in x['hashtags']])

    详情(我用的是谷歌colab):

    import pandas as pd
    a=pd.Series([ {'hashtags': [], 'symbols': [], 'user_mentions': [], 'urls': [{'url': '', 'expanded_url': 'https://twitter.com/i/web/status/1460865425911205891', 'display_url': 'twitter.com/i/web/status/1…', 'indices': [117, 140]}]}
    ,{'hashtags': [{'text': 'carbon', 'indices': [17, 24]}, {'text': 'nature', 'indices': [48, 55]}], 'symbols': [], 'user_mentions': [], 'urls': [{'url': '', 'expanded_url': 'https://rpujolviven.blogspot.com/2012/10/infinite-growth.html', 'display_url': 'rpujolviven.blogspot.com/2012/10/infini…', 'indices': [61, 84]}, {'url': '', 'expanded_url': 'https...'}]}
    ,{'hashtags': [{'text': 'Lincoln', 'indices': [69, 77]}], 'symbols': [], 'user_mentions': [], 'urls': [{'url': '', 'expanded_url': 'https://thelincolnite.co.uk/2021/11/professor-duncan-french-the-juggernaut-of-climate-politics-rolls-on/', 'display_url': 'thelincolnite.co.uk/2021/11/profes…', 'indices': [78, 101]}]}
    ,{'hashtags': [{'text': 'CBDC', 'indices':[107, 112]}], 'symbols': [], 'user_mentions': [], 'urls': [{'url': '', 'expanded_url': 'https://twitter.com/i/web/status/1460865388699344900', 'display_url': 'twitter.com/i/web/status/1…', 'indices': [115, 138]}]}])
    df = a.to_frame(name="entities")
    df['entities']
    df['entities'].map(lambda x:[i['text'] for i in x['hashtags']])
    

    输出:

    0    {'hashtags': [], 'symbols': [], 'user_mentions...
    1    {'hashtags': [{'text': 'carbon', 'indices': [1...
    2    {'hashtags': [{'text': 'Lincoln', 'indices': [...
    3    {'hashtags': [{'text': 'CBDC', 'indices': [107...
    Name: entities, dtype: object
    
    0                  []
    1    [carbon, nature]
    2           [Lincoln]
    3              [CBDC]
    Name: entities, dtype: object
    

    参考: https://pandas.pydata.org/docs/reference/api/pandas.Series.map.html

    【讨论】:

    • 感谢您的回复。如果我执行上述操作,我会收到以下错误。 in (x) 2 #tweets['hashtags'].apply(lambda x: [d['text'] for d in x]) 3 pd.set_option('max_colwidth ', 400) ----> 4 df['entities'].map(lambda x:[i['t​​ext'] for i in x['hashtags']]) TypeError: string indices must be integers
    • 嗯,是否可以查明是哪个数据行导致了错误?因为我在您的帖子中尝试了您的数据(4 个数据行),它对我来说效果很好。我猜有些数据行可能不符合您在帖子中给出的格式。
    • 我还编辑了答案以显示我的代码详细信息,希望对您有所帮助。
    猜你喜欢
    • 2020-03-26
    • 2020-03-17
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多