Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
for i,row in train_df.iterrows():
     if row['Age']>0 and row['Age']<=10:
            if row['Pclass']==1 :
                sp11=sp11+row['Survived']
                cp11=cp11+1
            elif row['Pclass']==2:
                sp12=sp12+row['Survived']
                cp12=cp12+1
            else:
                sp13=sp13+row['Survived']
                cp13=cp13+1
    if row['Age']>10 and row['Age']<=20:
            if row['Pclass']==1 :
                sp21=sp21+row['Survived']
                cp21=cp21+1
            elif row['Pclass']==2:
                sp22=sp22+row['Survived']
                cp22=cp22+1
            else:
                sp23=sp23+row['Survived']
                cp23=cp23+1
                
    if row['Age']>20 and row['Age']<=30:
            if row['Pclass']==1 :
                sp31=sp31+row['Survived']
                cp31=cp31+1
            elif row['Pclass']==2:
                sp32=sp32+row['Survived']
                cp32=cp32+1
            else:
                sp33=sp33+row['Survived']
                cp33=cp33+1
    if row['Age']>30 and row['Age']<=40:
            if row['Pclass']==1 :
                sp41=sp41+row['Survived']
                cp41=cp41+1
            elif row['Pclass']==2:
                sp42=sp42+row['Survived']
                cp42=cp42+1
            else:
                sp43=sp43+row['Survived']
                cp43=cp43+1
                
    if row['Age']>40 and row['Age']<=50:
            if row['Pclass']==1 :
                sp51=sp51+row['Survived']
                cp51=cp51+1
            elif row['Pclass']==2:
                sp52=sp52+row['Survived']
                cp52=cp52+1
            else:
                sp13=sp13+row['Survived']
                cp13=cp13+1
    if row['Age']>50 and row['Age']<=60:
            if row['Pclass']==1 :
                sp61=sp61+row['Survived']
                cp61=cp61+1
            elif row['Pclass']==2:
                sp62=sp62+row['Survived']
                cp62=cp62+1
            else:
                sp63=sp63+row['Survived']
                cp63=cp63+1


What I have tried:

I tried adjusting the spacing but I am getting the following error again and again.

Output:
File "<tokenize>", line 65
    if row['Age']>10 and row['Age']<=20:
    ^
IndentationError: unindent does not match any outer indentation level
Posted
Updated 15-Jan-20 2:05am

Quote:
I tried adjusting the spacing but I am getting the following error again and again.

The problem is the number of spaces
Python
for i,row in train_df.iterrows():        # in this loop
     if row['Age']>0 and row['Age']<=10: # the first inner statement is indented with 5 spaces
            if row['Pclass']==1 :
                sp11=sp11+row['Survived']
                cp11=cp11+1
            elif row['Pclass']==2:
                sp12=sp12+row['Survived']
                cp12=cp12+1
            else:
                sp13=sp13+row['Survived']
                cp13=cp13+1
    if row['Age']>10 and row['Age']<=20:  # but the second is indented with 4 spaces

but anything inside the loop must have the same indentation.
 
Share this answer
 
v2
Check the code really carefully: they may be spaces and tabs in there, so the indentation looks right, but doesn't actually match.
Editors can work with tabs, which are "seen" as spaces at intervals defined by the editor (and frequently variable in the app settings)
If the indentation the editor is showing you does not match what python expects then teh indentation levels will not match and you will get this error.

Check your editor, and see if it can "show marks" which may help you find tab characters, or convert tabs to spaces which means the output should always match what you see on teh screen.
 
Share this answer
 
Code with proper indentation

Python
for i,row in train_df.iterrows():
	if row['Age']>0 and row['Age']<=10:
		if row['Pclass']==1 :
			sp11=sp11+row['Survived']
			cp11=cp11+1
		elif row['Pclass']==2:
			sp12=sp12+row['Survived']
			cp12=cp12+1
		else:
			sp13=sp13+row['Survived']
			cp13=cp13+1
	if row['Age']>10 and row['Age']<=20:
		if row['Pclass']==1 :
			sp21=sp21+row['Survived']
			cp21=cp21+1
		elif row['Pclass']==2:
			sp22=sp22+row['Survived']
			cp22=cp22+1
		else:
			sp23=sp23+row['Survived']
			cp23=cp23+1

	if row['Age']>20 and row['Age']<=30:
		if row['Pclass']==1 :
			sp31=sp31+row['Survived']
			cp31=cp31+1
		elif row['Pclass']==2:
			sp32=sp32+row['Survived']
			cp32=cp32+1
		else:
			sp33=sp33+row['Survived']
			cp33=cp33+1
	if row['Age']>30 and row['Age']<=40:
		if row['Pclass']==1 :
			sp41=sp41+row['Survived']
			cp41=cp41+1
		elif row['Pclass']==2:
			sp42=sp42+row['Survived']
			cp42=cp42+1
		else:
			sp43=sp43+row['Survived']
			cp43=cp43+1

	if row['Age']>40 and row['Age']<=50:
		if row['Pclass']==1 :
			sp51=sp51+row['Survived']
			cp51=cp51+1
		elif row['Pclass']==2:
			sp52=sp52+row['Survived']
			cp52=cp52+1
		else:
			sp13=sp13+row['Survived']
			cp13=cp13+1
	if row['Age']>50 and row['Age']<=60:
		if row['Pclass']==1 :
			sp61=sp61+row['Survived']
			cp61=cp61+1
		elif row['Pclass']==2:
			sp62=sp62+row['Survived']
			cp62=cp62+1
		else:
			sp63=sp63+row['Survived']
			cp63=cp63+1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900