繁体   English   中英

数据帧中特定行的总和(Pandas)

[英]Sum of specific rows in a dataframe (Pandas)

我给出了一组以下数据:

week  A      B      C      D      E
1     243    857    393    621    194
2     644    576    534    792    207
3     946    252    453    547    436
4     560    100    864    663    949
5     712    734    308    385    303

我被要求找到指定行/指定周数的每列的总和,然后将这些数字绘制到条形图上以比较AE。

假设我有我需要的行(例如df.iloc[2:4,:] ),接下来我该怎么办? 我的假设是我需要创建一个包含每一列总和的单行掩码,但我不知道我是怎么做的。

我知道如何做最后一步(即.plot(kind='bar' ),我只需要知道中间步骤是什么,以获得我需要的总和。

您可以使用位置选择ilocsumSeries.plot.bar

df.iloc[2:4].sum().plot.bar()

graph1

或者如果想要按索引名称(这里是几周)选择,请使用loc

df.loc[2:4].sum().plot.bar()

graph2

区别是iloc排除最后位置:

print (df.loc[2:4])
        A    B    C    D    E
week                         
2     644  576  534  792  207
3     946  252  453  547  436
4     560  100  864  663  949

print (df.iloc[2:4])
        A    B    C    D    E
week                         
3     946  252  453  547  436
4     560  100  864  663  949

如果还需要按位置过滤列:

df.iloc[2:4, :4].sum().plot.bar()  

并按名称(周):

df.loc[2:4, list('ABCD')].sum().plot.bar()

您需要做的就是在您的数据子集上调用.sum()

df.iloc[2:4,:].sum()

返回:

week       7
A       1506
B        352
C       1317
D       1210
E       1385
dtype: int64

此外,对于绘图,我认为你可以摆脱week列(因为周数的总和不太可能意味着什么):

df.iloc[2:4,1:].sum().plot(kind='bar')
# or
df[list('ABCDE')].iloc[2:4].sum().plot(kind='bar')

情节

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM