3 回答

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以嘗試對(duì) 進(jìn)行分組,uid并在之后獲取其他列的最大值get_dummies
(pd.get_dummies(my_df ,columns=['days'],prefix='',prefix_sep='')
.groupby('uid',sort=False).max())
或者用pivot替代:
my_df.pivot_table(index="uid",columns="days",aggfunc='size',fill_value=0)
D1 D2 D3 D4
uid
4 1 0 0 1
3 0 0 1 1
6 0 1 1 0
1 0 1 0 0

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊
您可以使用crosstab+ clip:
df = pd.crosstab(my_df['uid'], my_df['days']).clip(upper=1)
days D1 D2 D3 D4
uid
1 0 1 0 0
3 0 0 1 1
4 1 0 0 1
6 0 1 1 0

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊
嘗試這個(gè)選項(xiàng):
ss = pd.get_dummies(my_df, columns=['days']).groupby(['uid'], as_index=False).sum()
print(ss)
輸出:
uid days_D1 days_D2 days_D3 days_D4
0 1 0 1 0 0
1 3 0 0 1 1
2 4 1 0 0 1
3 6 0 1 1 0
添加回答
舉報(bào)