在python

时间:2019-01-07 04:31:37

标签: python-3.x pandas csv

pandas.core.frame.DataFrame出现以下格式。如何从中获取每个字段并写入csv文件? csv的标头位于dataframe('uuid','name'等)的数据中

df1=
        total                                               data
    0      14  {'id': '489', 'uuid': 'cddcf03f-fff2-4f38-830e...
    1      14  {'id': '4662', 'uuid': '2fef9a5f-2a81-48ea-807...
    2      14  {'id': '4745', 'uuid': '1b478538-cf21-4c59-ba4...
    3      14  {'id': '3585', 'uuid': '3209a1ce-8960-4426-81b...
    4      14  {'id': '1956', 'uuid': 'bfd6b735-9ce4-45a8-bb6...
    5      14  {'id': '4132', 'uuid': '06cdd721-9eb4-41e8-9fa...
    6      14  {'id': '12335', 'uuid': '7e01a60f-6a39-4da0-a6...
    7      14  {'id': '13161', 'uuid': '67bfbab0-8269-4d07-83...
    8      14  {'id': '2510', 'uuid': '1d0fc6f8-a4c7-4d53-be2...
    9      14  {'id': '4404', 'uuid': '04e5ff20-bbc9-4ee6-8a5...
    10     14  {'id': '117', 'uuid': '4922ad75-b5ce-4c4e-81bd...
    11     14  {'id': '1281', 'uuid': 'a0c34d30-bce1-4962-bd8...
    12     14  {'id': '3115', 'uuid': '23c310cf-510b-481c-bae...
    13     14  {'id': '4698', 'uuid': '3a4063a3-2d91-4d10-bb4...


type(df1)=pandas.core.frame.DataFrame

data=df1['data']
type(data)=pandas.core.series.Series

1 个答案:

答案 0 :(得分:0)

joinpopDataFrame构造函数一起使用:

df = df.join(pd.DataFrame(df.pop('data').values.tolist()))
print (df)
    total     id                     uuid
0      14    489  cddcf03f-fff2-4f38-830e
1      14   4662   2fef9a5f-2a81-48ea-807
2      14   4745   1b478538-cf21-4c59-ba4
3      14   3585   3209a1ce-8960-4426-81b
4      14   1956   bfd6b735-9ce4-45a8-bb6
5      14   4132   06cdd721-9eb4-41e8-9fa
6      14  12335    7e01a60f-6a39-4da0-a6
7      14  13161    67bfbab0-8269-4d07-83
8      14   2510   1d0fc6f8-a4c7-4d53-be2
9      14   4404   04e5ff20-bbc9-4ee6-8a5
10     14    117  4922ad75-b5ce-4c4e-81bd
11     14   1281   a0c34d30-bce1-4962-bd8
12     14   3115   23c310cf-510b-481c-bae
13     14   4698   3a4063a3-2d91-4d10-bb4

如果performance不重要的解决方案:

df = df.join(df.pop('data').apply(pd.Series))