I have two tables similar to these:
JavaScript
x
20
20
1
df_1 = pd.DataFrame({'id': [1,1,1,1,1,2,2,2,2,2], 'x': [0,1,2,3,4,5,6,7,8,9]})
2
3
id x
4
0 1 0
5
1 1 1
6
2 1 2
7
3 1 3
8
4 1 4
9
5 2 5
10
6 2 6
11
7 2 7
12
8 2 8
13
9 2 9
14
15
df_2 = pd.DataFrame({'y': [10,100]}, index=[1,2])
16
17
y
18
1 10
19
2 100
20
My goal is to multiply df['x']
by df['y']
based on id
. My current solution works, but it seems to me that there should be a more efficient/elegant way of doing this.
This is my code:
JavaScript
1
16
16
1
df_comb = pd.merge(df_1, df_2, left_on='id', right_index=True)
2
x_new = df_comb['x'] * df_comb['y']
3
df_1['x_new'] = x_new.to_numpy()
4
5
id x x_new
6
0 1 0 0
7
1 1 1 10
8
2 1 2 20
9
3 1 3 30
10
4 1 4 40
11
5 2 5 500
12
6 2 6 600
13
7 2 7 700
14
8 2 8 800
15
9 2 9 900
16
Advertisement
Answer
You can use map
to recover the multiplication factor from df_2
, then mul
to the x
column:
JavaScript
1
2
1
df_1['x_new'] = df_1['id'].map(df_2['y']).mul(df_1['x'])
2
output:
JavaScript
1
12
12
1
id x x_new
2
0 1 0 0
3
1 1 1 10
4
2 1 2 20
5
3 1 3 30
6
4 1 4 40
7
5 2 5 500
8
6 2 6 600
9
7 2 7 700
10
8 2 8 800
11
9 2 9 900
12