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,620 points
55 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 65,820 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
314 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
31 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
45 views
+1 vote
1 answer

How to print an error in Python?

For Python 2.6 and later and Python ...READ MORE

answered Aug 23, 2018 in Python by Priyaj
• 56,100 points
55 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
19 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
• 65,820 points
637 views
0 votes
1 answer

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

I guess you are using speech_recognition module and missing ...READ MORE

answered Nov 28, 2018 in Python by Omkar
• 65,820 points
1,685 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.