简体   繁体   中英

Convert list type dictionary to pandas dataframe - python

I want to convert dictionary which has two rows. the values are in the first row and keys are in the second row. Here is my dictionary.

{'data': [['1', 'Male', ['a,b,c'], 'USA'],['2', 'Male', ['r,g,e'], 'JAPAN'],['3', 'Female', ['f,r,b'], 'UK']],
 'columns': ['id', 'gender', 'array_userid' ,'location']}

I want to convert it into a pandas data framelike below:

     id     gender   array_userid   location
0    1      Male     ['a,b,c']      USA
1    2      Male     ['r,g,e']      JAPAN
3    3      Female   ['f,r,b']      UK

Any help will be appreciated. thanks in advanced.

Use DataFrame constructor with select values of dictionary:

d = {'data': [['1', 'Male', ['a,b,c'], 'USA'],['2', 'Male', ['r,g,e'], 'JAPAN'],['3', 'Female', ['f,r,b'], 'UK']],
 'columns': ['id', 'gender', 'array_userid' ,'location']}

df = pd.DataFrame(data=d['data'], columns=d['columns'])
print (df)
  id  gender array_userid location
0  1    Male      [a,b,c]      USA
1  2    Male      [r,g,e]    JAPAN
2  3  Female      [f,r,b]       UK

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