如何对同一区域的价值求和?


import numpy as np
import pandas as pd
pd.options.display.max_rows = 20
np.random.seed(12345)
import matplotlib.pyplot as plt
plt.rc('figure', figsize=(10, 6))
np.set_printoptions(precision=4, suppress=True)

我制作了一个DataFrame代码;

frame = pd.DataFrame(np.arange(15).reshape((5, 3)),
index=[['a', 'a', 'b', 'b', 'c']],
columns=[['ps', 'ys','cw'],
])
frame.columns.names = ['region']

结果如下;

region  ps      ys      cw
a       0       1       2
a       3       4       5
b       6       7       8
b       9       10      11
c       12      13      14

我想知道每个人访问每个地区的次数。所以我做了一个代码。

def calling(index,columns):
frame.loc('index')
c = frame.sum(columns)
print(index, c)

在我的目的中,当我写调用(a,cw(时,输出是(a,7(。此外,调用('c','ys'(生成(c,13(,而调用('b','ps'(则生成(b,15(。但结果并没有出来。我该如何更改?

您需要在函数中对数据帧进行子集设置,首先按索引,然后按列:

def calling(index,column):
c = frame.loc[index][[column]].sum()[0]
print(index, c)

您需要将列提供为字符串而不是变量:

calling('a','cw')
a 7

如果我理解正确,您正在尝试按索引对数据帧进行分组,因此可以使用以下内容:

frame.groupby(frame.index).sum()

最新更新