【问题标题】:Extracting dictionary values from a pandas dataframe从熊猫数据框中提取字典值
【发布时间】:2017-08-28 22:03:23
【问题描述】:

我需要从我从 .json 文件导入的数据集中添加一个特征。

这就是它的样子:

f1 = pd.read_json('https://raw.githubusercontent.com/ansymo/msr2013-bug_dataset/master/data/v02/eclipse/short_desc.json')

print(f1.head())


                                               short_desc
1       [{'when': 1002742486, 'what': 'Usability issue...
10      [{'when': 1002742495, 'what': 'API - VCM event...
100     [{'when': 1002742586, 'what': 'Would like a wa...
10000   [{'when': 1014113227, 'what': 'getter/setter c...
100001  [{'when': 1118743999, 'what': 'Create Help Ind...

本质上,我需要将 'short_desc' 作为列名,并使用其正下方的字符串值填充它:'Usability issue...

到目前为止,我已经尝试了以下方法:

f1['desc'] = pd.DataFrame([x for x in f1['short_desc']])

Wrong number of items passed 19, placement implies 1

有没有不使用循环的简单方法来完成此任务?有人能指出这个新手正确的方向吗?

【问题讨论】:

    标签: python pandas dictionary dataframe


    【解决方案1】:

    不要初始化数据框并尝试将其分配给列 - 列应为 pd.Series

    您应该直接分配列表推导,如下所示:

    f1['desc'] = [x[0]['what'] for x in f1['short_desc']]
    

    作为替代方案,我会提出一个不涉及任何 lambda 函数的解决方案,使用 operatorpd.Series.apply

    import operator
    
    f1['desc'] = f1.short_desc.apply(operator.itemgetter(0))\
                                 .apply(operator.itemgetter('what'))
    print(f1.desc.head())
    
    1           Usability issue with external editors (1GE6IRL)
    10                   API - VCM event notification (1G8G6RR)
    100       Would like a way to take a write lock on a tea...
    10000     getter/setter code generation drops "F" in ".....
    100001    Create Help Index Fails with seemingly incorre...
    Name: desc, dtype: object
    

    【讨论】:

    • 这就是让我发疯的原因,为什么我们得到 1、10、100 等。而没有 'short_desc' 和列标题。
    • @JohnWayne360 因为您正在打印一个系列。试试print(df.head())。你会明白的。
    • @JohnWayne360 有趣的是,当您从 Web 链接加载索引时,索引似乎就出现了。想要重置它?做f1 = f1.reset_index(drop=1)
    • @COLDSPEED 谢谢,你是最棒的!这完全符合我的需要。
    【解决方案2】:

    或者你可以试试apply(PS:apply考虑时间成本函数)

    f1['short_desc'].apply(pd.Series)[0].apply(pd.Series)
    
    Out[864]: 
                                                         what        when   who
    1         Usability issue with external editors (1GE6IRL)  1002742486    21
    10                 API - VCM event notification (1G8G6RR)  1002742495    10
    100     Would like a way to take a write lock on a tea...  1002742586    24
    10000   getter/setter code generation drops "F" in ".....  1014113227   331
    100001  Create Help Index Fails with seemingly incorre...  1118743999  9571
    

    【讨论】:

    • 谢谢,如果/当我稍后试图将“何时”与“什么”相匹配时,这个答案将对我有用。非常感谢!
    猜你喜欢
    • 2020-05-23
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 2016-01-14
    • 1970-01-01
    相关资源
    最近更新 更多