简体   繁体   中英

Multiplying columns in separate pandas dataframe based on similar column values

Say I have 2 data frames

df1 = pd.DataFrame({'alpha': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'], 
                    'number': [1, 2, 3, 4, 5, 6, 7, 8, 9]})
  alpha  number
0     A       1
1     A       2
2     A       3
3     B       4
4     B       5
5     B       6
6     C       7
7     C       8
8     C       9

df2 = pd.DataFrame({'alpha': ['A', 'B', 'C'],
                    'mult': [2, 3, 4]})    
  alpha  mult
0     A     2
1     B     3
2     C     4

And I want to create a 3rd dataframe which will multiply all of the values in df1 by the corresponding 'mult' value in df2 based on the specific alpha value. The solution would look like this:

alpha  soln
0     A     2
1     A     4
2     A     6
3     B    12
4     B    15
5     B    18
6     C    28
7     C    32
8     C    36

Any tips on how to do this easily?

The first thing I can think of is to merge the two dataframes and then do the multiplication with the merged dataframe:

tmp = df1.merge(df2)

tmp
#   alpha  number  mult
# 0     A       1     2
# 1     A       2     2
# 2     A       3     2
# 3     B       4     3
# 4     B       5     3
# 5     B       6     3
# 6     C       7     4
# 7     C       8     4
# 8     C       9     4

df1.soln = tmp.number * tmp.mult

This works, though I do feel like there should be a simpler, one-step way too.

EDIT - here is a way to do this in one line:

df1.soln = (df1.set_index("alpha").number * df2.set_index("alpha").mult).values

EDIT2 - here's another one-liner, similar to @scott-boston's comment:

df1.soln = df1.merge(df2).assign(soln=lambda df: df.number * df.mult).soln

map + multiply

Your join is based on a single column, where the key is unique in df2 , so map.

df1['soln'] = df1.number.mul(df1.alpha.map(df2.set_index('alpha').mult))

#  alpha  number  soln
#0     A       1     2
#1     A       2     4
#2     A       3     6
#3     B       4    12
#4     B       5    15
#5     B       6    18
#6     C       7    28
#7     C       8    32
#8     C       9    36

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM