1 回答
TA贡献1848条经验 获得超2个赞
split然后您可以尝试explode填充crosstab每个 id 的缺失行
df1 = df.loc[df['feature']=='colour']
# slice out the row do not need to unnest
df2 = df.drop(df1.index)
df2['feature'] = df2['feature_value'].str.split('<->')
s = df2.explode('feature')
s = pd.crosstab(s['id'],s['feature']).stack().reset_index(name='feature_value')
out = pd.concat([df1,s]).sort_values('id')
out
Out[356]:
id feature feature_value
0 1 colour blue
0 1 cd_player 1
1 1 sat_nav 1
2 1 sub_woofer 0
3 1 usb_port 1
2 2 colour red
4 2 cd_player 1
5 2 sat_nav 0
6 2 sub_woofer 0
7 2 usb_port 1
4 3 colour red
8 3 cd_player 1
9 3 sat_nav 1
10 3 sub_woofer 1
11 3 usb_port 0
添加回答
举报