我想计算数据帧中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:
我不确定
pivot_table
在这里是如何应用的。在在
pivot_table
中:index
参数指定的列中具有相同的数据。。。在aggfunc
决定如何将多行聚合为一行。在但是您的pivot表有一个索引
ssname
,并且您的DataFrame在ssname列中没有重复项,因此pivot表的索引没有多个匹配的行,因此在pivot_表中不会发生聚合到一行中。你可以在这里看到:现在看看如果指定的索引
^{pr2}$ssaname
有重复项会发生什么:现在,在pivot\u表的
aaa
行中出现了聚合,因为原始数据帧中有两个aaa
行:多行是垂直聚合的,换句话说,aggfunc操作上下列,而不是跨行操作:
默认情况下,pivot_表使用
np.mean
聚合多行,10和30的平均值为20,1和3的平均值为2,因此在pivot_表中得到以下行:您可以指定不同的聚合函数:
但是
aggfunc
只适用于DataFrame中与pivot表中指定的索引匹配的多行。在以下是pivot_表为索引指定多个列的示例:
在原始数据帧中,有两行的列值被指定为透视表的索引},它们的值是相同的;因此它们的数据被聚合到索引对面的一行:
ssaname
和{aaa 1
。输出不会费心列出aaa
两次,但结果确实是:相关问题 更多 >
编程相关推荐