Python 将列总数附加到 Pandas DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20804673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Appending column totals to a Pandas DataFrame
提问by user3132783
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。追加表示每列总和的行(具有给定索引值)的最简单方法是什么?
回答by David M.
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)
回答by George Luft
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 Seriesobject (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.
也许有人知道更简单的方法。
回答by ideate
To add a Totalcolumn which is the sum across the row:
要添加一Total列,该列是该行的总和:
df['Total'] = df.sum(axis=1)
回答by BjoernL.
To add a row with column-totals:
要添加带有列总计的行:
df.loc['Total']= df.sum()
回答by Eli
You can use the appendmethod 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'))
回答by Poudel
This gives total on both rows and columns:
这给出了行和列的总数:
import numpy as np
import pandas as pd
df = pd.DataFrame({'a': [10,20],'b':[100,200],'c': ['a','b']})
df.loc['Column_Total']= df.sum(numeric_only=True, axis=0)
df.loc[:,'Row_Total'] = df.sum(numeric_only=True, axis=1)
print(df)
a b c Row_Total
0 10.0 100.0 a 110.0
1 20.0 200.0 b 220.0
Column_Total 30.0 300.0 NaN 330.0
回答by Biplab Malakar
- Calculate sum and convert result into list(axis=1:row wise sum, axis=0:column wise sum)
- Add result of step-1, to the existing dataFrame with new name
- 计算总和并将结果转换为列表(axis=1:row wise sum,axis=0:column wise sum)
- 将步骤 1 的结果添加到具有新名称的现有数据帧中
new_sum_col = list(df.sum(axis=1))
df['new_col_name'] = new_sum_col

