如何访问“熊猫”系列中的词典?

时间:2020-08-05 19:41:26

标签: python json pandas normalization

found a neat one-liner用于解析和标准化Pandas数据框中已经存在的JSON数据。但是,我正在处理系列,因此在更新代码时遇到了一些麻烦。我收到一个错误消息,说'list' object has no attribute 'items'

这是更新的代码:

data_normalized = pd.json_normalize(previous_df)

print(data_normalized.head())
# 0      [{'measurement': '5f21dda5018de300122aebed', '...
# 1      [{'measurement': '5f21dda5018de300122aebed', '...
# 2      [{'measurement': '5f21cca293a38f0012b21216', '...

# for x in data_normalized.pop(0).items(): print(x)
# This throws an error too ^

data_normalized = (pd.concat({pd.json_normalize(x) for x in data_normalized.pop(0).items()})
                   .reset_index(level=1, drop=True)
                   .join(data_normalized)
                   .reset_index(drop=True))

我不确定第一个.reset_index()是否引起此错误。如果不是,如何访问data_normalized系列中的词典?或者我如何能够规范系列data_normalized

0 个答案:

没有答案
相关问题