python DataFrame将字典列拆分为多列

时间:2018-12-07 06:28:19

标签: python pandas dataframe

该列的外观如下:

0 [{'id':18,'name':'Drama'},{'id':10769,'n ...

1 [{'id':18,'name':'Drama'},{'id':10749,'n ...

2 [{'id':35,'name':'Comedy'},{'id':27,'nam ...

3 [{'id':18,'name':'Drama'}]

4 [{'id':99,'name':'Documentary'}]

5 [{'id':18,'name':'Drama'},{'id':28,'name ...

6 [{'id':10749,'name':'Romance'},{'id':18,...

我希望看到每种类型的带有bool值的ID列:

index id = 18 id = 10769 id = 35 id = 27 ...

0 1 1 0 0 ...

1 1 0 0 0 ...

2 0 0 1 1 ...

3 1 0 0 0 ...

...

1 个答案:

答案 0 :(得分:0)

使用列表理解和扁平化,然后使用DataFrame构造函数:

df = pd.DataFrame({'col':[[{'id': 18, 'name': 'Drama'}, {'id': 10769}], 
                          [{'id': 99, 'name': 'Documentary'}]]})

print (type(df.loc[0, 'col']))
<class 'list'>

df = pd.DataFrame([y for x in df['col'] for y in x])
print (df)
      id         name
0     18        Drama
1  10769          NaN
2     99  Documentary

#alternative
#df = pd.concat([pd.DataFrame(x) for x in df['col']], ignore_index=True)