【发布时间】:2020-10-20 15:57:30
【问题描述】:
我在数据框 df 中有一个列 authors,其中值首先在字典中,然后将字典添加到列表中。然后将列表存储在列中。如:
[{'family': 'Yaisy',
'given': 'Lisa',
'affiliation': [{'name': 'Department of Sciences, Faculty Sciences, University of Science'}]},
{'family': 'Kite',
'given': 'Hume',
'affiliation': [{'name': 'Department of Sciences, Science and Technology'}]},
{'family': 'Jones',
'given name': 'Mike',
'localId': 'aza',
'affiliation': [{'name': 'Department of Health, Science and Technology'}]},
{'family': 'abc',
'given name': 'xyz',
'affiliation': [{'name': 'Health Sciences, University of Science'}]}]
我想将此列表分成不同的列,其中键作为列名,值作为列值。由于键有重复的名称,我可以为每个列名添加 1,2,3 作为后缀。我尝试了this question中建议的解决方案
df.join(pd.json_normalize(df.authors))
但是,我首先需要将列表更改为一个简单的字典,然后使用上面的解决方案。所以我尝试展平列表以获取字典并将它们存储在同一列中:
df.author = [y for x in df.author for y in x]
但是在这里我得到了值的长度与索引的长度不匹配的错误。
谁能帮我解决这个问题?谢谢!
【问题讨论】:
标签: python pandas dataframe dictionary