First time posting; apologies for formatting errors. I have a data set that contains age ranges in separate columns, and I'm trying to create a new column based on a string evaluation of the AGE_OPERATOR_TXT column:
I've tried using .apply() functions, lambda, for loops with iterrows(), etc... but either I can't get anything to return, or the function returns a series with ALL of the rows:
def multum_age_ops(s):
if s == "<":
return data['AGE_LOW_NBR'] + " + " + data['AGE_UNIT_DISP']
else:
return 0
data['age_op_test'] = data['AGE_OPERATOR_TXT'].apply(multum_age_ops)
I would expect that the column returned would actually look something like:
age_ops_test
0 0
1 18 + years
2 1 + months
3 4 + months
4 4 + months
What I'm getting is:
age_ops_test
0 0
1 0 18\n1 18\n2 1\n3 ...
2 0 18\n1 18\n2 1\n3 ...
3 0 18\n1 18\n2 1\n3 ...
4 0 18\n1 18\n2 1\n3 ...
5 0 18\n1 18\n2 1\n3 ...
6 0 18\n1 18\n2 1\n3 ...
Any help is appreciated.
return data['AGE_LOW_NBR'] + " + " + data['AGE_UNIT_DISP']that's a series...axis=1. Perhaps consider just concatenating across by element using the series syntax?data['age_op_test'] = data['AGE_OPERATOR_TXT'].apply(multum_age_ops, axis=1)but it returns an "unexpected argument" error.elseclause, returns the first column. Then a third that evaluates as above and returns the second column in theelseclause.