I keep getting the error message SyntaxError: invalid syntax
and I would like to know (1) why this is and (2) how to fix my function so that it does what I want.
I have a pandas dataframe that looks like this:
d = {'Relationship': ['Male', 'Female','Spouse','Spouse','Male','Spouse','Male','Male','Male','Spouse','Female'], 'Sex': ['Male', 'Female','Female','Male','Male','Female','Male','Male','Male','Female','Female']}
df = pd.DataFrame(data=d)
df
Relationship Sex
Male Male
Female Female
Spouse Female
Spouse Male
Male Male
Spouse Female
Male Male
Male Male
Male Male
Spouse Female
Female Female
And what I want is for each instance of Spouse
to be filled in with the opposite sex listed in df['Sex']
. So the df should look like this:
df
Relationship Sex
Male Male
Female Female
Male Female
Female Male
Male Male
Male Female
Male Male
Male Male
Male Male
Male Female
Female Female
This is the function I've written:
def typex(column):
if column['Relationship']!='Spouse' & column['Sex']! ='Female':
return 'Male'
elif column['Relationship']!='Spouse' & column['Sex']! ='Male':
return 'Female'
df.loc[:,'Relationship'] = df.apply(typex, axis=1)