(数据样本和问题末尾的尝试)

使用这样的数据框:

    Type    Class   Area    Decision 
0   A       1       North   Yes 
1   B       1       North   Yes 
2   C       2       South   No 
3   A       3       South   No 
4   B       3       South   No 
5   C       1       South   No 
6   A       2       North   Yes 
7   B       3       South   Yes 
8   B       1       North   No 
9   C       1       East    No 
10  C       2       West    Yes  

我怎样才能找到每种类型的百分比 [A, B, C, D]属于各个区域 [North, South, East, West] ?

期望的输出:
    North   South   East    West 
A   0.66    0.33    0       0 
B   0.5     0.5     0       0 
C   0       0.5     0.25    0.25 

到目前为止,我最好的尝试是:
df_attempt1= df.groupby(['Area', 'Type'])['Type'].aggregate('count').unstack().T 

返回:
Area  East  North  South  West 
Type                           
A      NaN    2.0    1.0   NaN 
B      NaN    2.0    2.0   NaN 
C      1.0    NaN    2.0   1.0 

我想我可以通过计算边距中的总和并附加 0对于遗漏的观察,但我真的很感激关于更优雅方法的建议。

感谢您的任何建议!

代码:

import pandas as pd 
 
df = pd.DataFrame( 
    { 
        "Type": {0: "A", 1: "B", 2: "C", 3: "A", 4: "B", 5: "C", 6: "A", 7: "B", 8: "B", 9: "C", 10: "C"}, 
        "Class": {0: 1, 1: 1, 2: 2, 3: 3, 4: 3, 5: 1, 6: 2, 7: 3, 8: 1, 9: 1, 10: 2}, 
        "Area": {0: "North", 1: "North", 2: "South", 3: "South", 4: "South", 5: "South", 6: "North", 7: "South", 8: "North", 9: "East", 10: "West"}, 
        "Decision": {0: "Yes", 1: "Yes", 2: "No", 3: "No", 4: "No", 5: "No", 6: "Yes", 7: "Yes", 8: "No", 9: "No", 10: "Yes"}, 
    } 
) 
 
dfg = df[['Area', 'Type']].groupby(['Area']).agg('count').unstack() 
 
df_attempt1 = df.groupby(['Area', 'Type'])['Type'].aggregate('count').unstack().T 

请您参考如下方法:

您可以使用功能 crosstab :

pd.crosstab(index=df['Type'], columns=df['Area'], normalize='index') 
输出:
Area  East     North     South  West 
Type                                 
A     0.00  0.666667  0.333333  0.00 
B     0.00  0.500000  0.500000  0.00 
C     0.25  0.000000  0.500000  0.25 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!