熊猫:添加百分比列

时间:2021-04-07 07:33:33

标签: python pandas dataframe

有 Pandas DataFrame 为:

print(df)

call_id   calling_number   call_status
1          123             BUSY
2          456             BUSY
3          789             BUSY
4          123             NO_ANSWERED
5          456             NO_ANSWERED
6          789             NO_ANSWERED

在这种情况下,具有不同 call_status 的记录(比如“错误”或其他什么,我无法预测),值可能会出现在数据框中。我需要为这样的值即时添加一个新列。 我已经应用了 pivot_table() 函数并得到了我想要的结果:

df1 = df.pivot_table(df,index='calling_number',columns='status_code', aggfunc = 'count').fillna(0).astype('int64')

calling_number    ANSWERED  BUSY   NO_ANSWER  
123               0          1      1
456               0          1      1
789               0          1      1

现在我需要再添加一列,其中包含具有给定 call_number 的已接电话的百分比,计算为 ANSWERED 与总数的比率。 源数据帧 'df' 可能不包含 call_status = 'ANSWERED' 的条目,因此在这种情况下,百分比列自然应为零值。

预期结果是:

calling_number    ANSWERED  BUSY   NO_ANSWER  ANS_PERC(%)
    123               0          1      1      0
    456               0          1      1      0
    789               0          1      1      0 

2 个答案:

答案 0 :(得分:1)

使用crosstab

df1 = pd.crosstab(df['calling_number'], df['status_code'])

或者如果需要通过 count 函数使用 NaN 和添加参数 pivot_table 来排除 fill_value=0

df1 = df.pivot_table(df,
               index='calling_number',
               columns='status_code', 
               aggfunc = 'count', 
               fill_value=0)

然后对于每行的比率除法求和值:

df1 = df1.div(df1.sum(axis=1), axis=0)
print (df1)
                ANSWERED      BUSY  NO_ANSWER
calling_number                               
123             0.333333  0.333333   0.333333
456             0.333333  0.333333   0.333333
789             0.333333  0.333333   0.333333

编辑:要添加可能不存在的某些类别,请使用 DataFrame.reindex:

df1 = (pd.crosstab(df['calling_number'], df['call_status'])
         .reindex(columns=['ANSWERED','BUSY','NO_ANSWERED'], fill_value=0))

df1['ANS_PERC(%)'] = df1['ANSWERED'].div(df1['ANSWERED'].sum()).fillna(0)
print (df1)
call_status     ANSWERED  BUSY  NO_ANSWERED  ANS_PERC(%)
calling_number                                          
123                    0     1            1          0.0
456                    0     1            1          0.0
789                    0     1            1          0.0

如果需要每行总数:

df1['ANS_PERC(%)'] = df1['ANSWERED'].div(df1.sum(axis=1))
print (df1)
call_status     ANSWERED  BUSY  NO_ANSWERED  ANS_PERC(%)
calling_number                                          
123                    0     1            1          0.0
456                    0     1            1          0.0
789                    0     1            1          0.0

编辑 1:

Soluton 将一些错误的值替换为 ERROR

print (df)
   call_id  calling_number  call_status
0        1             123          ttt
1        2             456         BUSY
2        3             789         BUSY
3        4             123  NO_ANSWERED
4        5             456  NO_ANSWERED
5        6             789  NO_ANSWERED

L = ['ANSWERED', 'BUSY', 'NO_ANSWERED']
df['call_status'] = df['call_status'].where(df['call_status'].isin(L), 'ERROR')
print (df)
0        1             123        ERROR
1        2             456         BUSY
2        3             789         BUSY
3        4             123  NO_ANSWERED
4        5             456  NO_ANSWERED
5        6             789  NO_ANSWERED
df1 = (pd.crosstab(df['calling_number'], df['call_status'])
         .reindex(columns=L + ['ERROR'], fill_value=0))

df1['ANS_PERC(%)'] = df1['ANSWERED'].div(df1.sum(axis=1))
print (df1)
call_status     ANSWERED  BUSY  NO_ANSWERED  ERROR  ANS_PERC(%)
calling_number                                                 
123                    0     0            1      1          0.0
456                    0     1            1      0          0.0
789                    0     1            1      0          0.0

答案 1 :(得分:0)

我喜欢 cross_tab 的想法,但我喜欢列操作,因此很容易参考:

    # define a function to capture all the other call_statuses into one bucket 
def tester(x):
    if x not in ['ANSWERED', 'BUSY', 'NO_ANSWERED']:
        return 'OTHER' 
    else:
        return x
    
#capture the simplified status in a new column
df['refined_status'] = df['call_status'].apply(tester)


#Do the pivot (or cross tab) to capture the sums:
df1= df.pivot_table(values="call_id", index = 'calling_number', columns='refined_status', aggfunc='count')

#Apply a division to get the percentages:
df1["TOTAL"] = df1[['ANSWERED', 'BUSY', 'NO_ANSWERED', 'OTHER']].sum(axis=1)
df1["ANS_PERC"] = df1["ANSWERED"]/df1.TOTAL * 100

print(df1)