它分为A类和B类。df :Category A A A B B CODE U-01 U-02 U-03 U-04 U-05n1 0 1 0 2 nann2 1 1 0 nan nann3 3 0 nan 0 2我想根据标准值“0”、“>0”和 nan 计算值。我想要的输出表如下所示:Category A BStandard 0 >0 nan 0 >0 nan n1 2 1 0 0 1 1 n2 1 2 0 0 0 2 n3 0 1 1 1 1 0example) "n1-> Standard : 0 " is A&U-01, A&U-03 So, 2请帮我,,
1 回答
桃花长相依
TA贡献1860条经验 获得超8个赞
用于DataFrame.unstack
reshape DataFrame
for Series with Multiindex
,然后将值 gretaer 转换0
为>0
,将缺失值替换为字符串并使用 reshape bynan
计算它们:SeriesGroupBy.value_counts
Series.unstack
df1 = (df.unstack()
.mask(lambda x: x.gt(0), '>0')
.fillna('nan')
.groupby(level=[0, 2])
.value_counts()
.unstack([0,2], fill_value=0)
.rename(columns={0:'0'}))
print (df1)
Category A B
0 >0 nan >0 nan 0
n1 2 1 0 1 1 0
n2 1 2 0 0 2 0
n3 1 1 1 1 0 1
添加回答
举报
0/150
提交
取消