【问题标题】:csv file with one Dictionary column带有一个 Dictionary 列的 csv 文件
【发布时间】:2021-10-12 17:59:47
【问题描述】:

我有一个包含多列的 CSV 文件,其中一列有字典元素,如何将此字典键扩展到列

数据框 enter image description here

尝试应用 pd.Series

enter image description here

输出: enter image description here

它仍然没有被分成列。

参考了这个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) 为这个数据框工作,但不适用于我原来的数据框。

【问题讨论】:

    标签: python dataframe dictionary


    【解决方案1】:

    尝试使用 json 规范化:

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

    【讨论】:

    • 它不工作,它给出错误:AttributeError: 'str' object has no attribute 'values'
    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 2022-11-04
    相关资源
    最近更新 更多