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,670 points
589 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,020 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, 2019 in Python by Shri
37,179 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
9,613 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 in Python by Niroj
• 45,040 points
258 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
419 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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,207 views
0 votes
0 answers
+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
• 69,020 points
5,406 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 in Python by rahul
• 360 points
24,222 views