Python pandas axis error

–1 vote

I have the following python script:

sd=df.loc[:,[ "No. of Inmates benefitted byElementary Education",
"No. of Inmates benefitted by Adult Education",
"No. of Inmates benefitted by Higher Education", "No. of Inmates benefitted by Computer Course"]]
print(df[sd.sum(axis=1)]) -->this is giving row sum with axis =1 

Why is the following statement failing? with axis = 0, doesn't it mean it is column-wise sum?
print(df[sd.sum(axis=0)]) # Statement failing.

Jan 8, 2019 in Python by digger
• 26,740 points
1,857 views

1 answer to this question.

0 votes

When you are applying sum() you don't have to use df[] in df[sd.sum()] as you have already stored the required columns into sd dataframe. Hence to overcome this issue, please try by giving sd.sum(axis=0) instead of df[sd.sum(axis=0)]

Example:

import pandas as pd
df=pd.read_csv("prisoners.csv")
print(df.columns)
sd=df.loc[: ,['No. of Inmates benefitted by Elementary Education','No. of Inmates benefitted by Adult Education','No. of Inmates benefitted by Higher Education']]
print(sd)
print(sd.sum(axis=1)) #row wise
print(sd.sum(axis=0)) #column wise
answered Jan 8, 2019 by Omkar
• 69,210 points

Related Questions In Python

0 votes
2 answers

Python Pandas error: AttributeError: 'DataFrame' object has no attribute 'rows'

Try this: data=pd.read_csv('/your file name', delim_whitespace=Tru ...READ MORE

answered Dec 10, 2020 in Python by anonymous
• 82,880 points
130,350 views
0 votes
1 answer

Python error "pandas.errors.ParserError: Error tokenizing data"

Pandas do not take .xlsx files. Rename ...READ MORE

answered Jun 13, 2019 in Python by Rhea
18,726 views
0 votes
1 answer

Error:UnicodeDecodeError when reading CSV file in Pandas with Python

Hello @kartik, Try this: import pandas as pd df = ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,880 points
7,412 views
+2 votes
2 answers

Error while printing hello world in python.

You must be trying this command in ...READ MORE

answered Mar 31, 2018 in Python by GandalfDwhite
• 1,320 points
5,393 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,067 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,486 views
0 votes
1 answer

Python argparse error "NameError: name 'file' is not defined"

The right datatype for destination in argpasrse module ...READ MORE

answered Nov 28, 2018 in Python by Omkar
• 69,210 points
13,004 views
0 votes
3 answers

Python error "NameError: name 'sr' is not defined"

NameError: name 'xx' is not defined Python knows ...READ MORE

answered Mar 19, 2020 in Python by rahul
• 360 points
41,403 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP