0

So I am trying to create a new row in my dataframe that adds up the sums for only column A B and C and have the means for column D and E. I have tried doing something like:

df.loc['totals'] = df['daily budget', 'weekly budget', 'monthly budget'].sum() + df['average spent monthly','average spent yearly'].mean()

It didnt work. Should I try to define each value by doing something like

sum1 = df['daily budget'].sum()
sum2 = df['weekly budget'].sum()
sum3 = ....
mean1 =df['average monthly'].mean()
mean2 = ....

And fill in the cell?

1
  • You should give a minimal-reproducible-example. Without data, it is hard to understand what do you want. Commented Sep 23, 2020 at 19:55

1 Answer 1

1

If you want to go with your approach then this should work.

sum1 = df['daily budget'] + df['weekly budget'] + df['monthly budget']

mean1 = (df['average spent monthly'] + df['average spent yearly'])/2

df['total'] = sum1 + mean1
Sign up to request clarification or add additional context in comments.

4 Comments

I get a nan result for all columns using this method.
check your data types with df.dtypes to verify that those columns are numeric data types
They are all float64 except for one which is an object because its a bunch of names. On the bottom of the list it says dtype:Object though. I though i resolved this problem when I used pd.to_numeric on columns containing numbers.
Is it possible to add a snippet of the dataframe so we can see?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.