I have to a dataframe df in each row I have some columns I want to make subtraction, columns_to_sub, some tag column called 'absorb' and some columns that I don't want to change. I want to subtract the values of columns_to_sub by a row that is on another dataframe and is indexed by the tag 'absorb'. Here is a non functional example of what I want:
import pandas as pd
import numpy as np
data = np.hstack((np.random.randint(0,10,20).reshape(-1,1),np.random.rand(20,3)))
df = pd.DataFrame(data,columns=['absorb','a','b','c'])
columns_to_sub = ['a','b']
means = df.groupby('absorb')[columns_to_sub].mean()
#This result is not what I want, because the subtraction is strange
df[columns_to_sub] = df[columns_to_sub] - means.loc[df.absorb,columns_to_sub]
How do I fix this code?