答案 0 :(得分:5)
这意味着将每列除以列的最后一个值(All
行的值),并将其转换为float
。
您可以通过以下方式查看:
def percConvert(ser):
print (ser)
print (ser[-1])
return ser / float(ser[-1])
但可以使用crosstab
中的参数normalize=0
简化解决方案(仅删除一列的列):
df1 = pd.crosstab(data['Credit_History'],data['Loan_Status'], margins=True, normalize=0)
样品:
np.random.seed(123)
N = 100
data = pd.DataFrame({'Loan_Status': np.random.choice(['Y','N'], N),
'Credit_History':np.random.choice([0., 1.], N)})
#print (data)
def percConvert(ser):
return ser / float(ser[-1])
df1 = pd.crosstab(data['Credit_History'],data['Loan_Status'], margins=True, normalize=0)
print (df1)
Loan_Status N Y
Credit_History
0.0 0.489362 0.510638
1.0 0.415094 0.584906
All 0.450000 0.550000
df1 = pd.crosstab(data['Credit_History'],data['Loan_Status'], margins=True)
.apply(percConvert, axis=1)
print (df1)
Loan_Status N Y All
Credit_History
0.0 0.489362 0.510638 1.0
1.0 0.415094 0.584906 1.0
All 0.450000 0.550000 1.0