I have two dataframes (df1
, df2
):
x id
35 4
55 3
92 2
99 5
and
id x val
1 (0.0, 50.0] 1.2
2 (90.0, inf] 0.5
3 (0.0, 50.0] 8.9
3 (50.0, 90.0] 9.9
4 (0.0, 50.0] 4.3
4 (50.0, 90.0] 1.1
4 (90.0, inf] 2.9
5 (50.0, 90.0] 3.2
5 (90.0, inf] 5.1
Want to add a new column x_new
in the first dataframe, df1
, which values depends on the lookup-table from the second dataframe, df2
. According to the id
and the value of x
, there is a special multiplier, to get the new value x_new
:
x id x_new
35 4 35*4.3
55 3 55*9.9
92 2 ...
99 5 ...
The value ranges in the second dataframe were created with a pandas cut:
df2 = df.groupby(['id', pd.cut(df.x, [0,50,90,np.inf])]).apply(lambda x: np.average(x['var1']/x['var2'], weights=x['var1'])).reset_index(name='val')
My idea is starting with the pandas built in lookup function:
df1['x_new'] = df.lookup(df.index, df['id'])
Don't know how to get it work.
Also see my previous question for more information about the code.