3

The pandas.core.frame.DataFrame appearsin the following format. How to get each fields from this and write to csv file? The headers of csv is in the data of dataframe('uuid' ,'name' etc)

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
9113303
  • 852
  • 1
  • 16
  • 30
  • 1
    Provide complete input and output samples. – meW Jan 07 '19 at 04:54
  • if the dtype of `data` column is a dictionary, `df.join(df['data'].apply(pd.Series))` should work . you can then drop the `data` column or keep it as you want – anky Jan 07 '19 at 06:16

1 Answers1

2

Use join with pop and DataFrame constructor:

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

Solution if performance is not important:

df = df.join(df.pop('data').apply(pd.Series))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252