简体   繁体   中英

How do I represent a list within a dict as a row in a Pandas dataframe?

I have a list of dict, which looks like this:

[
 {
   'project': 'one',
   'name': 'test',
   'samples': 
     [ 
      {'timestamp': 12, 'value': None},
      {'timestamp': 23, 'value': None}
     ]
  },
 {
   'project': 'two',
   'name': 'best',
   'samples': 
     [ 
      {'timestamp': 12, 'value': None},
      {'timestamp': 23, 'value': None}
     ]
  }
]

I am trying to build a dataframe which would look like this:

project, name, timestamp, values
one, test, 12, none
one, test, 23, none
two, best, 12, none
two, best, 23, none 

When I try just putting the dict directly into a dataframe, I get something like this:

project, name, sample
one, test, [{timestamp:12, value:none},timestamp:23, value:none}]

Any help would be appreciated!

Check json_normalize , l is your list here

from pandas.io.json import json_normalize
json_normalize(l, 'samples', ['name', 'project',['value', 'timestamp']],errors='ignore').drop('value.timestamp',1)
Out[195]: 
   timestamp value project  name
0         12  None     one  test
1         23  None     one  test
2         12  None     two  best
3         23  None     two  best

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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