简体   繁体   English

带有一个字典列的 csv 文件

[英]csv file with one Dictionary column

I have one CSV file with multiple columns, one of the columns have dictionary elements, how to expand this dictionary keys to columns我有一个包含多列的 CSV 文件,其中一列有字典元素,如何将此字典键扩展到列

Data Frame enter image description here数据框在此处输入图像描述

Tried applying pd.Series尝试应用 pd.Series

enter image description here在此处输入图片说明

output: enter image description here输出:在此处输入图像描述

Still it's not getting split into columns.它仍然没有被分成几列。

referred this stackoverflow solution: df = pd.DataFrame({'id':[1,2,3], 'col1':[{'a':1, 'b':2, 'c':3}, {'a':3, 'b':4, 'c':5}, {'a':None, 'b':5, 'c':6}]}) df.set_index('id').col1.apply(pd.Series) working for this data frame, but not working for my original one.引用了这个 stackoverflow 解决方案: df = pd.DataFrame({'id':[1,2,3], 'col1':[{'a':1, 'b':2, 'c':3}, { 'a':3, 'b':4, 'c':5}, {'a':None, 'b':5, 'c':6}]}) df.set_index('id')。 col1.apply(pd.Series) 适用于此数据框,但不适用于我的原始数据框。

Try using json normalise:尝试使用 json 规范化:

import pandas as pd
df2 = pd.json_normalize(df['address'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM