0

I'm trying to apply a lambda function to a pandas Dataframe that return the difference between each row and the max of that column.

It can be easily achieved by using a separate variable and setting that to the max of the column, but I'm curious how it can be done in a single line of code.

import pandas as pd

df = pd.DataFrame({
    'measure': [i for i in range(0,10)]
})

col_max = df.measure.max()
df['diff_from_max'] = df.apply(lambda x: col_max - x['measure'], axis=1)
2
  • df['diff_from_max'] = df['measure'].max() - df['measure'] you dont need apply for this Commented Apr 12, 2020 at 16:31
  • Thanks, @anky, I need to use apply as it's a simplified version of the question I'm trying to solve. I have to call other functions as part of my lambda function and use apply to use value from other columns including max of the column. Commented Apr 12, 2020 at 16:41

2 Answers 2

2

We usually do

max_df=df.max()-df

df=df.join(max_df.add_prefix('diff_max_')

To fix your code since the apply is not need here

col_max = df.measure.max()
df['diff_from_max'] = col_max-df['measure']
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but it's still two line! :)
output is exactly what I have in df. I'm only looking for a single line solution with apply, if it's possible.
@MehdiZare df.apply(lambda x: df.measure.max() - x['measure'], axis=1)?
Thanks! Now I see what I missed, I used x instead of df to get the max in lambda.
1

I think apply() is not required here. You can simply use following line :

df['diff_from_max'] = df['measure'].max() - df['measure']

Comments

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.