小编VIK*_*WAL的帖子

为pandas数据透视表中的每个值列定义aggfunc

正在尝试生成具有多个"值"列的数据透视表.我知道我可以使用aggfunc以我想要的方式聚合值,但是如果我不想对两列进行求和或者平均而是我需要一列的总和而另一列的意思.那么使用熊猫是否可以这样做?

df = pd.DataFrame({
          'A' : ['one', 'one', 'two', 'three'] * 6,
          'B' : ['A', 'B', 'C'] * 8,
          'C' : ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 4,
          'D' : np.random.randn(24),
          'E' : np.random.randn(24)
})
Run Code Online (Sandbox Code Playgroud)

现在这将获得一个带有sum的数据透视表:

pd.pivot_table(df, values=['D','E'], rows=['B'], aggfunc=np.sum)
Run Code Online (Sandbox Code Playgroud)

这意味着:

pd.pivot_table(df, values=['D','E'], rows=['B'], aggfunc=np.mean)
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到和的D意思E

希望我的问题很清楚.

python python-2.7 pandas

20
推荐指数
2
解决办法
2万
查看次数

pandas数据帧中数据的条件重置

我正在寻找一种有效的方法(如果可能的话,没有循环/迭代)使用输入a和b获取我的输出.a是具有随机数的数组,b是定义重置点的数组.

a = pd.DataFrame([2, 5, 4, 1, 6, 6, 4, 7])
b = pd.DataFrame([1, 0, 0, 1, 0, 0, 1, 0])
Run Code Online (Sandbox Code Playgroud)

输出:

[2, 2, 2, 1, 1, 1, 4, 4]
Run Code Online (Sandbox Code Playgroud)

python-2.7 pandas

0
推荐指数
1
解决办法
92
查看次数

标签 统计

pandas ×2

python-2.7 ×2

python ×1