Skip to content Skip to sidebar Skip to footer

Pandas: How Can I Pass A Column Name To A Function That Can Then Be Used In 'apply'?

I have a function: def logit_categorisation(row, column_name): val = 0 if row[column_name] > 0.6: val = 1 elif 0.4 < row[column_name] < 0.6:

Solution 1:

Probably better not to do it that way. Your function only uses the row and column name to operate on a single value. So just make your function accept the value directly (that is, the value you currently call row[column_name]) as its argument, and then you can do:

df[name_of_column + '_category'] = df[name_of_column].map(logit_categorization)

Post a Comment for "Pandas: How Can I Pass A Column Name To A Function That Can Then Be Used In 'apply'?"