我可以用组计数成功地填充我的新列,但我怀疑有一种更简单的方法:
# How do I simplify this?
def f(gr):
return pd.Series([gr['class_name'].count()] * gr.shape[0], index=gr.index)
df['class_size'] = df.groupby("class_name").apply(f).reset_index(level=0, drop=True)
column_list = ['class_name', 'class_size']
df[column_list].head(5)
获取:
我想你需要^{} :
或:
What is the difference between size and count in pandas?
根据您的数据帧形状,您也可以只对groupby进行计数:
相关问题 更多 >
编程相关推荐