2 回答
TA贡献1886条经验 获得超2个赞
使用crosstab
with作为新索引名称,然后为byDataFrame.rename_axis
添加新列,最后将索引转换为column by :0 position
DataFrame.insert
DataFrame.reset_index
df = pd.crosstab(df['items'], df['status']).rename_axis(columns=None, index='unique')
df.insert(0, 'count', df.sum(axis=1))
df = df.reset_index()
print (df)
unique count fail pass
0 bike 3 1 2
1 car 3 2 1
2 jet 4 4 0
如果count应该是最后一列,则可以使用margin参数并删除最后一行:
df = (pd.crosstab(df['items'], df['status'],
margins=True,
margins_name='count')
.rename_axis(columns=None, index='unique')
.iloc[:-1]
.reset_index())
print (df)
unique fail pass count
0 bike 1 2 3
1 car 2 1 3
2 jet 4 0 4
TA贡献1821条经验 获得超4个赞
您可以单独获取值并与以下组合pd.concat:
A = df.groupby("items").size().rename("count")
A
items
bike 3
car 3
jet 4
Name: count, dtype: int64
B = (
df.groupby(["items", "status"])
.size()
.unstack(fill_value=0)
.rename_axis(columns=None)
)
B
fail pass
items
bike 1 2
car 2 1
jet 4 0
pd.concat((A, B), axis=1).reset_index()
items count fail pass
0 bike 3 1 2
1 car 3 2 1
2 jet 4 4 0
添加回答
举报