将列总计追加到Pandas DataFrame

时间:2022-11-13 15:51:36

I have a DataFrame with numerical values. What is the simplest way of appending a row (with a given index value) that represents the sum of each column?

我有一个带数值的DataFrame。附加行(具有给定索引值)表示每列总和的最简单方法是什么?

5 个解决方案

#1


45  

To add a Total column which is the sum across the row:

要添加“总计”列,该列是行中的总和:

df['Total'] = df.sum(axis=1)

#2


42  

To add a row with column-totals:

要添加包含列总计的行:

df.loc['Total']= df.sum()

#3


6  

One way is to create a DataFrame with the column sums, and use DataFrame.append(...). For example:

一种方法是使用列总和创建DataFrame,并使用DataFrame.append(...)。例如:

import numpy as np
import pandas as pd
# Create some sample data
df = pd.DataFrame({"A": np.random.randn(5), "B": np.random.randn(5)}) 
# Sum the columns:
sum_row = {col: df[col].sum() for col in df}
# Turn the sums into a DataFrame with one row with an index of 'Total':
sum_df = pd.DataFrame(sum_row, index=["Total"])
# Now append the row:
df = df.append(sum_df)

#4


3  

I have done it this way:

我这样做了:

df = pd.concat([df,pd.DataFrame(df.sum(axis=0),columns=['Grand Total']).T])

this will add a column of totals for each row:

这将为每一行添加一列总计:

df = pd.concat([df,pd.DataFrame(df.sum(axis=1),columns=['Total'])],axis=1)

It seems a little annoying to have to turn the Series object (or in the answer above, dict) back into a DataFrame and then append it, but it does work for my purpose.

将Series对象(或上面的答案,dict)转换回DataFrame然后追加它似乎有点烦人,但它确实适用于我的目的。

It seems like this should just be a method of the DataFrame - like pivot_table has margins.

看起来这应该只是DataFrame的一种方法 - 就像pivot_table有边距一样。

Perhaps someone knows of an easier way.

也许有人知道一种更简单的方法。

#5


0  

You can use the append method to add a series with the same index as the dataframe to the dataframe. For example:

您可以使用append方法将与数据框具有相同索引的系列添加到数据框。例如:

df.append(pd.Series(df.sum(),name='Total'))

#1


45  

To add a Total column which is the sum across the row:

要添加“总计”列,该列是行中的总和:

df['Total'] = df.sum(axis=1)

#2


42  

To add a row with column-totals:

要添加包含列总计的行:

df.loc['Total']= df.sum()

#3


6  

One way is to create a DataFrame with the column sums, and use DataFrame.append(...). For example:

一种方法是使用列总和创建DataFrame,并使用DataFrame.append(...)。例如:

import numpy as np
import pandas as pd
# Create some sample data
df = pd.DataFrame({"A": np.random.randn(5), "B": np.random.randn(5)}) 
# Sum the columns:
sum_row = {col: df[col].sum() for col in df}
# Turn the sums into a DataFrame with one row with an index of 'Total':
sum_df = pd.DataFrame(sum_row, index=["Total"])
# Now append the row:
df = df.append(sum_df)

#4


3  

I have done it this way:

我这样做了:

df = pd.concat([df,pd.DataFrame(df.sum(axis=0),columns=['Grand Total']).T])

this will add a column of totals for each row:

这将为每一行添加一列总计:

df = pd.concat([df,pd.DataFrame(df.sum(axis=1),columns=['Total'])],axis=1)

It seems a little annoying to have to turn the Series object (or in the answer above, dict) back into a DataFrame and then append it, but it does work for my purpose.

将Series对象(或上面的答案,dict)转换回DataFrame然后追加它似乎有点烦人,但它确实适用于我的目的。

It seems like this should just be a method of the DataFrame - like pivot_table has margins.

看起来这应该只是DataFrame的一种方法 - 就像pivot_table有边距一样。

Perhaps someone knows of an easier way.

也许有人知道一种更简单的方法。

#5


0  

You can use the append method to add a series with the same index as the dataframe to the dataframe. For example:

您可以使用append方法将与数据框具有相同索引的系列添加到数据框。例如:

df.append(pd.Series(df.sum(),name='Total'))