Python pandas axis error

0 votes

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 in Python by digger
• 27,640 points
150 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 by Omkar
• 67,460 points

Related Questions In Python

0 votes
1 answer

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

There is no attribute called “rows”. The ...READ MORE

answered Mar 28 in Python by Shri
3,024 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 in Python by Rhea
567 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
96 views
0 votes
2 answers

Indentation Error in Python

Use tabs instead of spaces. This is ...READ MORE

answered Feb 14 in Python by Shashank
• 1,350 points
79 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 226 views
+4 votes
6 answers
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
• 67,460 points
1,329 views
0 votes
2 answers