I am trying to do a conditional replacing of values in one column(age_cat) by values in another column(stillbirth) but it's giving me a type error
TypeError: '<' not supported between instances of 'str' and 'float'
basically, I need it to say age_cat is "SB" if report_stillbirth is Yes
'report_stillbirth' | 'age_cat' |
---|---|
No | 1 |
Yes | 0 |
No | 2 |
No | 4 |
report_stillbirth is a string age_cat is an integer
df.loc[df['report_stillbirth'] == "Yes", 'age_cat'] = "SB"
I have tried to change the type of the age_cat to string with:
df['age_cat'] = df['age_cat'].astype(str)
CodePudding user response:
Try this:
import numpy as np
import pandas as pd
df['age_cat'] = np.where(df['report_stillbirth'] == 'Yes', 'SB', df['age_cat'])
Example:
import numpy as np
import pandas as pd
choices = ['Yes', 'No']
df = pd.DataFrame(
{
'report_stillbirth': np.random.choice(choices, 10),
'age_cat': np.random.randint(1, 15, 10)
}
)
print(df)
# prints:
#
# report_stillbirth age_cat
# 0 No 6
# 1 No 1
# 2 Yes 4
# 3 Yes 12
# 4 Yes 13
# 5 No 2
# 6 No 7
# 7 Yes 1
# 8 Yes 7
# 9 Yes 10