提取pandas列中字典的元素

qxsslcnc  于 2022-11-20  发布在  其他
关注(0)|答案(1)|浏览(114)

我一直在努力从一个字典中提取元素,放在一个Pandas专栏里,然后把这些元素放到几个新的专栏里。
我有一个由2列组成的DataFrame,即ID和数据(字典)。

ID  data
0   6602629924  {'@status': 'found', '@_fa': 'true', 'coredata...
1   55599317400 {'@status': 'found', '@_fa': 'true', 'coredata...
2   25652391600 {'@status': 'found', '@_fa': 'true', 'coredata...
3   11939875400 {'@status': 'found', '@_fa': 'true', 'coredata...
4   56140547500 {'@status': 'found', '@_fa': 'true', 'coredata...

例如,如果我想从一行中提取一个“affiliation”,我将使用下面这行代码来调用它:
data[1]["author-profile"]["affiliation-current"]["affiliation"]["ip-doc"]["afdispname"]
返回'De La Salle University'
但当涉及到整个专栏时,它就不起作用了。

new_df["affiliation"] = new_df['data']["author-profile"]["affiliation-current"]["affiliation"]["ip-doc"]["afdispname"]
new_df

 ---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-145-8abd5f976526> in <module>
----> 1 new_df["affiliation"] = new_df['data']["author-profile"]["affiliation-current"]["affiliation"]["ip-doc"]["afdispname"]
      2 new_df

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    866         key = com.apply_if_callable(key, self)
    867         try:
--> 868             result = self.index.get_value(self, key)
    869 
    870             if not is_scalar(result):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
   4373         try:
   4374             return self._engine.get_value(s, k,
-> 4375                                           tz=getattr(series.dtype, 'tz', None))
   4376         except KeyError as e1:
   4377             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()

KeyError: 'author-profile'

我做错了什么?

f45qwnt8

f45qwnt81#

使用json_normalize()

new_df=new_df.join(pd.json_normalize(new_df.pop('data')))

相关问题