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)
df['diff_from_max'] = df['measure'].max() - df['measure']you dont need apply for this