从pandas数据帧中提取字典值

2024-09-21 03:26:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从从.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…”。。。在

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

^{pr2}$

有没有不使用循环的简单方法来实现这一点?有人能给这个新手指点方向吗?在


Tags: 文件数据方法httpsjsonreadissue特性
2条回答

不要初始化数据帧并尝试将其分配给列-列应该是pd.Series。在

您应该直接指定列表理解,如下所示:

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

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

^{pr2}$

或者您可以尝试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

相关问题 更多 >

    热门问题