格劳比百分比计算熊猫

时间:2019-11-20 08:15:29

标签: pandas pandas-groupby

我有一个如下所示的数据框

Sector   Plot   Usage        Status             Area
A        1      Villa        Constructed        40
A        2      Residential  Constructed        50
A        3      Substation   Not_Constructed    20
A        4      Villa        Not_Constructed    60
A        5      Residential  Not_Constructed    30
A        6      Substation   Constructed        100
B        1      Villa        Constructed        80
B        2      Residential  Constructed        60
B        3      Substation   Not_Constructed    40
B        4      Villa        Not_Constructed    80
B        5      Residential  Not_Constructed    100
B        6      Substation   Constructed        40

从上面我想根据使用情况和状态来计算每个扇区的面积百分比。

预期的输出1:

Sector   %_Residential  %_Villa   %_Substation   %_Constructed  %_Non_Constructed  Total_area
A        26.67          33.33     40             63.33          27.67              300
B        40             40        20             45             55                 400

预期的输出2:

Sector    Total_Residential_area  %_Resid_constructed_area  %_Resid_Not_constructed_area
A         80                      16.67                     10
B         160                     15                        25

哪里

%_ Residential =居住区百分比 %_Constructed =建筑面积百分比 等等

1 个答案:

答案 0 :(得分:1)

使用:

#aggregate sum per 2 columns Sector and Usage
df1 = df.groupby(['Sector', 'Usage'])['Area'].sum()
#percentage by division of total per Sector
df1 = df1.div(df1.sum(level=0), level=0).unstack(fill_value=0).mul(100).add_prefix('%_')
#aggregate sum per 2 columns Sector and Status
df2 = df.groupby(['Sector', 'Status'])['Area'].sum()
df2 = df2.div(df2.sum(level=0), level=0).unstack(fill_value=0).mul(100).add_prefix('%_')
#total Area per Sector
s = df.groupby('Sector')['Area'].sum().rename('Total_area')
#join all together
dfA = pd.concat([df1, df2, s], axis=1).reset_index()
print (dfA)
  Sector  %_Residential  %_Substation    %_Villa  %_Constructed  \
0      A      26.666667          40.0  33.333333      63.333333   
1      B      40.000000          20.0  40.000000      45.000000   

   %_Not_Constructed  Total_area  
0          36.666667         300  
1          55.000000         400  
#filter only Residential rows
df4 = df[df['Usage'].eq('Residential')]
#aggregate sum per 2 columns Sector and Status
df5 = df4.groupby(['Sector', 'Status'])['Area'].sum()
#divide by total Sector from previous solution
df5 = df5.div(s, level=0).unstack(fill_value=0).mul(100).add_prefix('%_Resid_')
df6 = df4.groupby('Sector')['Area'].sum().rename('Total_Residential_area')
#join togetehr
dfB = pd.concat([df6, df5], axis=1).reset_index()
print (dfB)
  Sector  Total_Residential_area  %_Resid_Constructed  %_Resid_Not_Constructed
0      A                      80            16.666667                     10.0
1      B                     160            15.000000                     25.0