Use crosstab
with normalize=0
, then add prefix to columns by DataFrame.add_prefix
and add to original DataFrame by DataFrame.join
:
df1 = df.join(pd.crosstab(df['state'], df['class'], normalize=0).add_prefix('State_'),
on='state')
print (df1)
state class State_0 State_1
0 A 0 0.600000 0.400000
1 B 1 0.000000 1.000000
2 C 1 0.333333 0.666667
3 A 0 0.600000 0.400000
4 A 1 0.600000 0.400000
5 B 1 0.000000 1.000000
6 A 0 0.600000 0.400000
7 A 1 0.600000 0.400000
8 C 1 0.333333 0.666667
9 C 0 0.333333 0.666667
Last if need filter some columns:
df2 = df1.reindex(['State_0','State_1','class'], axis=1)
print (df2)
State_0 State_1 class
0 0.600000 0.400000 0
1 0.000000 1.000000 1
2 0.333333 0.666667 1
3 0.600000 0.400000 0
4 0.600000 0.400000 1
5 0.000000 1.000000 1
6 0.600000 0.400000 0
7 0.600000 0.400000 1
8 0.333333 0.666667 1
9 0.333333 0.666667 0
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…