简体   繁体   English

如何每第 4 行将 append 列总计到 pandas dataframe?

[英]How to append column total to pandas dataframe every 4th row?

I have the following dataframe with weekly numbers for column A, B and C:我有以下 dataframe 列 A、B 和 C 的每周编号:

week  A B C
0     1 0 0
1     1 0 1
2     0 1 0
3     1 1 1
4     1 0 0
5     0 0 1
6     0 1 0
7     1 1 1
8     1 0 0
9     0 0 1
10    0 1 0
11    1 1 1

and I want to append the dataframe with a row consisting of the monthly total for each column (so the previous 4 weeks together), with a wanted outcome something like this:我想 append dataframe 一行包含每列的每月总数(所以前 4 周一起),想要的结果是这样的:

week   A B C
0      1 0 0
1      1 0 1
2      0 1 0
3      1 1 1
total  3 2 2
4      1 0 0
5      0 0 1
6      1 1 0
7      2 1 0
total  4 2 1
8      1 0 0
9      0 0 1
10     0 0 0
11     1 0 1
total  2 0 2

I have used我用过

df.groupby(df.index // 4).sum(numeric_only=True, axis=0)

to get the monthly numbers but I do not know how to get it appended in the df.获取每月的数字,但我不知道如何将它附加到 df 中。 Any ideas?有任何想法吗? Thanks谢谢

pd.concat

Iterating through the groupby object allows us to append the total row to each sub-dataframe.遍历 groupby object 允许我们 append 到每个子数据帧的总行。 By passing this through a dictionary comprehension/ pd.concat we conveniently get month added as a level in the index to disambiguate the 'total' identifier in the index.通过字典理解/ pd.concat传递它,我们可以方便地将month添加为索引中的一个级别,以消除索引中的'total'标识符的歧义。

pd.concat({
    m: d.append(d.sum().rename('total'))
    for m, d in df.groupby(df.index // 4)
}, names=['month'])

             A  B  C
month week          
0     0      1  0  0
      1      1  0  1
      2      0  1  0
      3      1  1  1
      total  3  2  2
1     4      1  0  0
      5      0  0  1
      6      0  1  0
      7      1  1  1
      total  2  2  2
2     8      1  0  0
      9      0  0  1
      10     0  1  0
      11     1  1  1
      total  2  2  2

I assume that week in your data sample is a regular column (not index).我假设您的数据样本中的那一周是常规列(不是索引)。

Your code generates only "sum" rows for each group, but the proper solution is to:您的代码只为每个组生成“总和”行,但正确的解决方案是:

  • generate a "sum" row for the current group,为当前组生成一个“总和”行,
  • return the original group with this row appended.返回附加此行的原始组。

To do it define a function adding the "total" row to the current group and returning it:为此,定义一个 function 将“总”行添加到当前组并返回它:

def addTotal(grp):
    wrk = grp.loc[:, 'A':'C'].sum().append(
        pd.Series(['total'], index=['week'])).rename('total')
    return grp.append(wrk)

Then group the source DataFrame as you did so far and apply this function:然后像到目前为止一样对源 DataFrame 进行分组并应用此 function:

result = df.groupby(df.index // 4, group_keys=False)\
    .apply(addTotal).reset_index(drop=True)

(an additional step is to reset the index). (另一个步骤是重置索引)。

The result, for your data sample, is:对于您的数据样本,结果是:

     week  A  B  C
0       0  1  0  0
1       1  1  0  1
2       2  0  1  0
3       3  1  1  1
4   total  3  2  2
5       4  1  0  0
6       5  0  0  1
7       6  0  1  0
8       7  1  1  1
9   total  2  2  2
10      8  1  0  0
11      9  0  0  1
12     10  0  1  0
13     11  1  1  1
14  total  2  2  2

Note: Your data sample and the expected result are inconsistent.注意:您的数据样本与预期结果不一致。 Eg row for week == 7 , column A has:例如 row == 7 ,列A有:

  • 1 in data sample, 1在数据样本中,
  • but 2 in the expected result.2在预期的结果。

This explains some discrepancies between your expected result and mine.这解释了您的预期结果和我的预期结果之间的一些差异。

暂无
暂无

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

相关问题 如何在 pandas dataframe 中每隔 4 行 select 并计算滚动平均值 - How to select every 4th row in a pandas dataframe and calculate the rolling average 对于 pandas dataframe 中的列,从当前行计算前 4、8 和 12 行的列值的平均值? - For a column in pandas dataframe, calculate mean of column values in previous 4th, 8th and 12th row from the present row? 如何使用 MultiIndex append 将“总计”行添加到 pandas dataframe - How to append a "Total" row to pandas dataframe with MultiIndex 如何使用Python遍历第4行中的第4个像素? - How do I loop through every 4th pixel in every 4th row, using Python? 如何将 3 个 Pandas 数据帧合并到第 4 个数据帧以匹配列值名称? - How to Merge 3 Pandas DataFrames to a 4th DataFrame to match column value Name? 使用 Pandas DataFrame,如何保留每 7 行? - With a pandas DataFrame, how do I keep every 7th row? Python pandas dataframe 如何将 Z78E6221F6393D1356681DB398FtrCE4 作为第 6 行和第 6 行 - Python pandas dataframe how to output th as a column and tr as the row 我怎样才能总结熊猫数据帧中每 x 列和 x+2 列的列? - How can I sum up colum every x-th and x+2-th column in pandas dataframe? 如何为 Pandas 中的一列创建“总计”行 Dataframe - How To Create a "Total" Row for One Column in a Pandas Dataframe 如何在此文件中的每 4 个元素之后将 append 和 output 转换为 txt 文件 - How to append an output to a txt file after every 4th element in this file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM