1 回答
TA贡献1830条经验 获得超3个赞
使用SeriesGroupBy.nunique
:
df1 = df1.join(df.groupby('Variation')['id'].nunique().rename('Total id'))
print(df1)
end start step1 step2 Total id
Variation
A 3 2 2 2 3
B 0 0 2 1 2
如果之后需要列Variation:
c = ['id'] + df['steps'].unique().tolist()
df1 = (df1.join(df.groupby('Variation')['id'].nunique())
.reindex(columns=c)
.add_prefix('Total ')
.reset_index()
.rename_axis(None, axis=1))
print(df1)
Variation Total id Total start Total step1 Total step2 Total end
0 A 3 2 2 2 3
1 B 2 0 2 1 0
添加回答
举报