Pandas Pivot Table:计算相对于两列的百分比

2024-09-25 18:12:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想计算数据帧中2列的百分比。数据帧是:

df1.head()
     ssaname      ym  tch_block  call_drop  cell_name
0  AAAAAAAAA  201504          0         39        345
1  aaaaaaaaa  201505          2         48        291
2  bbbbbbbbb  201506          2         49        360
3  ccccccccc  201507          4         59        357
4  ddddddddd  201508         10         74        363

百分比应为tch_block*100/cell_name
尝试的命令是:

^{pr2}$

Tags: 数据namecellcallblockheaddrop百分比
1条回答
网友
1楼 · 发布于 2024-09-25 18:12:15

我不确定pivot_table在这里是如何应用的。在

pivot_table中:

  1. 如果数据帧中有多行。。。在
  2. 在pivot\u表的index参数指定的列中具有相同的数据。。。在
  3. 这些行被聚合成一行。在

aggfunc决定如何将多行聚合为一行。在

但是您的pivot表有一个索引ssname,并且您的DataFrame在ssname列中没有重复项,因此pivot表的索引没有多个匹配的行,因此在pivot_表中不会发生聚合到一行中。你可以在这里看到:

d = {
    'ssaname' : pd.Series(['aaa', 'bbb', 'ccc', 'ddd']),
    #'ym' : pd.Series(np.arange(201504, 201509) ),
    'tch_block' : pd.Series([1, 1, 1, 3,]),
    'call_drop' : pd.Series([10, 10, 10, 30,]),
    #'cell_name' : pd.Series([345, 291, 360, 357, 363])
}

df = pd.DataFrame(d)
print(df)

result = df.pivot_table(
    index = ['ssaname'],
    values = ['tch_block', 'call_drop'],
)

print(result)

  output: 
   call_drop ssaname  tch_block
0         10     aaa          1
1         10     bbb          1
2         10     ccc          1
3         30     ddd          3

         call_drop  tch_block
ssaname                      
aaa             10          1
bbb             10          1
ccc             10          1
ddd             30          3

现在看看如果指定的索引ssaname有重复项会发生什么:

^{pr2}$

现在,在pivot\u表的aaa行中出现了聚合,因为原始数据帧中有两个aaa行:

       call_drop ssaname  tch_block
    0         10     aaa          1
    ...
    ...
    3         30     aaa          3

多行是垂直聚合的,换句话说,aggfunc操作上下列,而不是跨行操作:

       call_drop ssaname  tch_block
    0         10     aaa          1
              ^                   ^                  
              |                   |
           aggfunc            aggfunc
              |                   |            
              V                   V
    3         30     aaa          3

默认情况下,pivot_表使用np.mean聚合多行,10和30的平均值为20,1和3的平均值为2,因此在pivot_表中得到以下行:

         call_drop  tch_block
ssaname                      
aaa             20          2

您可以指定不同的聚合函数:

d = {
    'ssaname' : pd.Series(['aaa', 'bbb', 'ccc', 'aaa']),
    #'ym' : pd.Series(np.arange(201504, 201509) ),
    'tch_block' : pd.Series([1, 1, 1, 3,]),
    'call_drop' : pd.Series([10, 10, 10, 30,]),
    #'cell_name' : pd.Series([345, 291, 360, 357, 363])
}

df = pd.DataFrame(d)
print(df)

result = df.pivot_table(
    index = ['ssaname'],
    values = ['tch_block', 'call_drop'],
    aggfunc = np.sum  #****HERE****
)

print(result)

 output: 
   call_drop ssaname  tch_block
0         10     aaa          1
1         10     bbb          1
2         10     ccc          1
3         30     aaa          3

         call_drop  tch_block
ssaname                      
aaa             40          4
bbb             10          1
ccc             10          1

但是aggfunc只适用于DataFrame中与pivot表中指定的索引匹配的多行。在

以下是pivot_表为索引指定多个列的示例:

d = {
    'ssaname' : pd.Series(['aaa', 'bbb', 'ccc', 'aaa', 'aaa']),
    #'ym' : pd.Series(np.arange(201504, 201509) ),
    'tch_block' : pd.Series([1, 1, 1, 1, 100]),
    'call_drop' : pd.Series([10, 10, 10, 30, 10]),
    #'cell_name' : pd.Series([345, 291, 360, 357, 363])
}

df = pd.DataFrame(d)
print(df)

result = df.pivot_table(
    index = ['ssaname', 'tch_block'],
    values = ['call_drop'],
    aggfunc = np.sum

)

print(result)

 output: 
   call_drop ssaname  tch_block
0         10     aaa          1
1         10     bbb          1
2         10     ccc          1
3         30     aaa          1
4         10     aaa        100
                   call_drop
ssaname tch_block           
aaa     1                 40
        100               10
bbb     1                 10
ccc     1                 10

在原始数据帧中,有两行的列值被指定为透视表的索引ssaname和{},它们的值是相同的;因此它们的数据被聚合到索引对面的一行:aaa 1。输出不会费心列出aaa两次,但结果确实是:

ssaname tch_block           
aaa     1                 40
aaa     100               10
bbb     1                 10
ccc     1                 10

相关问题 更多 >