Assuming the singular row to be excluded is known, one method for computing the overall sum of a dataframe excluding that row is as follows:
import pandas as pd
# create example dataframe
df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]})
print(df)
# identify and exclude row from dataframe
exclude_index = 2
df_exclude = df.drop(exclude_index)
print(df_exclude)
# compute overall sum of remaining dataframe
sum_exclude = df_exclude.sum().sum()
print(sum_exclude)
Explanation:
df
with two columns (A
and B
) and four rows.exclude_index = 2
) and create a new dataframe df_exclude
that excludes that row using the drop()
method.df_exclude
by first summing each column with .sum()
and then taking the sum of those sums with a second .sum()
function.sum_exclude
).Asked: 2021-12-28 11:00:00 +0000
Seen: 11 times
Last updated: Apr 13 '23