如何在python中为每个变量计算变量的摘要并保存为dataframe
我有一个熊猫数据框
Age_Bin Cat_Bin Outcome
Age1 Cat2 0
Age1 Cat1 1
Age2 Cat2 1
Age1 Cat1 1
Age2 Cat1 0
Age3 Cat1 0
Age3 Cat2 0
Age1 Cat1 1
Age3 Cat2 1
使用下面给定的代码计算每个变量的结果分布摘要,如下所示。你知道吗
Age\ U Bin变量示例
Age_Bin Outcome_0_cnt Outcome_1_cnt Total_cnt Outcome_0_cnt% Outcome_1_cnt%
Age1 1 3 4 1/4 3/5
Age2 1 1 2 1/4 1/5
Age3 2 1 3 2/4 1/5
这是使用下面的代码实现的
df1 = ( df.groupby(['Age_Bin','Outcome'])['Cat_Bin'] .size() .unstack(fill_value=0) .add_prefix('Outcome_') ) df = df1.assign(Total_cnt=lambda x: x.sum(1)).join(df1.div(df1.sum()).add_suffix('%'))
print (df)
Outcome Outcome_0 Outcome_1 Total_cnt Outcome_0% Outcome_1%
Age_Bin
Age1 1 3 4 0.25 0.6
Age2 1 1 2 0.25 0.2
Age3 2 1 3 0.50 0.2
除了上面的输出,我还需要在结果1%旁边再加一列Z。你知道吗
Z_Age= log(Outcome_1%/Outcome_0%).
然后根据给定的每个类别,将每个变量的Z值映射到原始df
Age_Bin Cat_Bin Outcome Z_Age Z_Cat
Age1 Cat2 0
Age1 Cat1 1
Age2 Cat2 1
Age1 Cat1 1
Age2 Cat1 0
Age3 Cat1 0
Age3 Cat2 0
Age1 Cat1 1
Age3 Cat2 1
用途:
然后添加:
相关问题 更多 >
编程相关推荐