5

How do I correctly reference another column value when using a Lambda in a pandas dataframe.

dfresult_tmp2['Retention_Rolling_temp'] = dfresult_tmp2['Retention_tmp'].apply(lambda x: x if x['Count Billings']/4 < 0.20 else '')

The above code gives me this error.

TypeError: 'float' object is not subscriptable

1 Answer 1

10
dfresult_tmp2['Retention_tmp'].apply(
    lambda x: x if x['Count Billings'] / 4 < 0.20 else ''
)

You are using pd.Series.apply which is different than pd.DataFrame.apply. In this case, you are iteratively passing a scalar value to the lambda. So some_scalar_x['Count Billings'] makes no sense.

Instead of telling you how to shoehorn your logic into an apply, I'll show you the vectorized versions instead

Option 1
pd.Series.where

dfresult_tmp2['Retention_tmp'] = \
    dfresult_tmp2['Retention_tmp'].where(
        dfresult_tmp2['Count Billings'] / 4 < .2, '')

Option 2
np.where

r = dfresult_tmp2['Retention_tmp'].values
b = dfresult_tmp2['Count Billings'].values
dfresult_tmp2['Retention_tmp'] = np.where(b / 4 < .2, r, '')

Option 3
apply
What you asked for but not what I'd recommend.

dfresult_tmp2['Retention_tmp'] = dfresult_tmp2.apply(
    lambda x: x['Retention_tmp'] if x['Count Billings'] / 4 < .2 else '',
    axis=1
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @piRSquared I went with Option 1. For Option 3 I tried but was still getting an error. IndexError: ('Count Billings', 'occurred at index ISONAME')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.