Python 如何在pandas中转换分类数据帧
我的数据框在下面Python 如何在pandas中转换分类数据帧,python,pandas,Python,Pandas,我的数据框在下面 Sex type M A W B W A M C 我想得到下面的结果 M W A B C 1 0 1 0 0 0 1 0 1 0 0 1 1 0 0 1 0 0 0 1 我试过了 df['Sex','type'].get_dummies() 但效果不好 我怎样才能得到这个结果?我想你需要: 此外,如果列名称中有一些重复项是必要的,groupbyby columnsaxis=1和level=0和aggregatemax: print (pd.ge
Sex type
M A
W B
W A
M C
我想得到下面的结果
M W A B C
1 0 1 0 0
0 1 0 1 0
0 1 1 0 0
1 0 0 0 1
我试过了
df['Sex','type'].get_dummies()
但效果不好
我怎样才能得到这个结果?我想你需要:
此外,如果列名称中有一些重复项是必要的,groupby
by columnsaxis=1
和level=0
和aggregatemax
:
print (pd.get_dummies(df, prefix_sep='', prefix='').groupby(axis=1, level=0).max())
A B C M W
0 1 0 0 1 0
1 0 1 0 0 1
2 1 0 0 0 1
3 0 0 1 1 0
print (pd.get_dummies(df, prefix_sep='', prefix='').groupby(axis=1, level=0).max())
A B C M W
0 1 0 0 1 0
1 0 1 0 0 1
2 1 0 0 0 1
3 0 0 1 1 0