I want to merge two columns to another dataframe based on Squad column df1
Squad
0 Arsenal
1 Aston Villa
2 Bournemouth
3 Brighton
4 Burnley
5 Chelsea
6 Crystal Palace
7 Everton
8 Leicester City
9 Liverpool
10 Manchester City
11 Manchester Utd
12 Newcastle Utd
13 Norwich City
14 Sheffield Utd
15 Southampton
16 Tottenham
17 Watford
18 West Ham
19 Wolves
df2
Rk Squad MP W D L GF GA GD Pts ... L GF GA GD Pts Pts/G xG xGA xGD xGD/90
0 1 Liverpool 19 18 1 0 52 16 36 55 ... 3 33 17 16 44 2.32 31.2 21.6 9.5 0.50
1 2 Manchester City 19 15 2 2 57 13 44 47 ... 7 45 22 23 34 1.79 45.5 19.5 26.0 1.37
2 3 Manchester Utd 19 10 7 2 40 17 23 37 ... 6 26 19 7 29 1.53 28.4 21.1 7.4 0.39
3 4 Chelsea 19 11 3 5 30 16 14 36 ... 7 39 38 1 30 1.58 29.2 27.2 2.0 0.10
4 5 Leicester City 19 11 4 4 35 17 18 37 ... 8 32 24 8 25 1.32 31.0 22.7 8.3 0.44
5 6 Tottenham 19 12 3 4 36 17 19 39 ... 7 25 30 -5 20 1.05 21.6 28.7 -7.1 -0.37
6 7 Wolves 19 8 7 4 27 19 8 31 ... 5 24 21 3 28 1.47 21.2 18.3 2.9 0.15
7 8 Arsenal 19 10 6 3 36 24 12 36 ... 7 20 24 -4 20 1.05 22.0 25.8 -3.8 -0.20
8 9 Sheffield Utd 19 10 3 6 24 15 9 33 ... 6 15 24 -9 21 1.11 15.3 28.2 -12.9 -0.68
9 10 Burnley 19 8 4 7 24 23 1 28 ... 7 19 27 -8 26 1.37 16.6 27.1 -10.4 -0.55
10 11 Southampton 19 6 3 10 21 35 -14 21 ... 6 30 25 5 31 1.63 30.8 24.9 5.9 0.31
11 12 Everton 19 8 7 4 24 21 3 31 ... 11 20 35 -15 18 0.95 21.8 24.9 -3.1 -0.16
12 13 Newcastle Utd 19 6 8 5 20 21 -1 26 ... 11 18 37 -19 18 0.95 15.0 30.3 -15.3 -0.81
13 14 Crystal Palace 19 6 5 8 15 20 -5 23 ... 9 16 30 -14 20 1.05 16.4 31.6 -15.2 -0.80
14 15 Brighton 19 5 7 7 20 27 -7 22 ... 8 19 27 -8 19 1.00 20.3 28.5 -8.2 -0.43
15 16 West Ham 19 6 4 9 30 33 -3 22 ... 10 19 29 -10 17 0.89 22.5 31.9 -9.4 -0.49
16 17 Aston Villa 19 7 3 9 22 30 -8 24 ... 12 19 37 -18 11 0.58 18.5 34.5 -16.0 -0.84
17 18 Bournemouth 19 5 6 8 22 30 -8 21 ... 14 18 35 -17 13 0.68 20.4 32.3 -11.9 -0.63
18 19 Watford 19 6 6 7 22 27 -5 24 ... 13 14 37 -23 10 0.53 18.5 29.9 -11.4 -0.60
19 20 Norwich City 19 4 3 12 19 37 -18 15 ... 15 7 38 -31 6 0.32 17.7 30.2 -12.6 -0.66
I want to create a new column on df1 and make a calculation that is, value of W column divide by value of MP column. I tried to merge W column to df1 but I got TypeError: Cannot convert bool to numpy.ndarray
df1 = pd.merge(df1, df2['Squad','W'], on='Squad',how='left')
TRY:
result = df1.merge(df2[['Squad','W', 'MP']], how='left')
result['new_col'] = result['W'] / result['MP']
NOTE:
Make sure to handle NAN and 0
before dividing.
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.