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
).Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-12-28 11:00:00 +0000
Seen: 11 times
Last updated: Apr 13 '23
How can we bring googlesheets data into a pyspark dataframe?
How can a portion of a file name be retrieved and stored in a DataFrame using Pandas?
How can we perform aggregate functions on particular datetime values in a Pandas DataFrame?
How can you display a Pandas Dataframe using a for loop?
How do you update a dataframe within a for loop in R after passing a list?
How can the list within a dataframe be transformed so that it becomes a binary data type?
What is the method for making a struct in a Spark dataframe less complex?