Python 3.x 更改数据帧中的值
我有一个数据帧:Python 3.x 更改数据帧中的值,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,我有一个数据帧: First name Last name Height weight unit 0 Merna Imel 185 89 kg 1 Taren Kuehn 189 123 lbs 2 Ginette Schutte 199 67 kg 3 Natasha Kibble 180 121 lbs 4
First name Last name Height weight unit
0 Merna Imel 185 89 kg
1 Taren Kuehn 189 123 lbs
2 Ginette Schutte 199 67 kg
3 Natasha Kibble 180 121 lbs
4 Lucrecia Berube 191 68 kg
5 Twana Bonebrake 156 138 lbs
6 Eugena Felberbaum 165 73 kg
7 Gussie Eddington 169 114 lbs
8 Siobhan Krupp 150 56 kg
9 Delcie Jiron 150 110 lbs
10 Allena Sandusky 166 87 kg
11 Dylan Verrill 166 174 lbs
12 Ambrose Pasillas 186 85 kg
13 Nannette Lehrer 195 207 lbs
14 Jenifer Mcgarrah 167 94 kg
15 Tarsha Jarnigan 162 189 lbs
16 Helen Macgregor 188 101 kg
17 Kara Belfiore 179 167 lbs
18 Harvey Quesinberry 168 74 kg
我想要的是改变它。因此,我需要所有单位谁有“磅”改变“重量”为公斤的功能“磅/2.2046”。然后删除高度列,而是创建一个新的BMI列,其中BMI使用公式“重量/高度^2”计算
请帮助使用with mask比较lbs
和多个常量值,对于BMI
将列分开,并使用**
进行平方:
df.loc[df['unit'].eq('lbs'), 'weight'] *= 0.454
df['BMI'] = (df.weight/df.Height)**2
print (df)
First name Last name Height weight unit BMI
0 Merna Imel 185 89.000 kg 0.231439
1 Taren Kuehn 189 55.842 lbs 0.087297
2 Ginette Schutte 199 67.000 kg 0.113356
3 Natasha Kibble 180 54.934 lbs 0.093140
4 Lucrecia Berube 191 68.000 kg 0.126751
5 Twana Bonebrake 156 62.652 lbs 0.161295
6 Eugena Felberbaum 165 73.000 kg 0.195739
7 Gussie Eddington 169 51.756 lbs 0.093788
8 Siobhan Krupp 150 56.000 kg 0.139378
9 Delcie Jiron 150 49.940 lbs 0.110845
10 Allena Sandusky 166 87.000 kg 0.274677
11 Dylan Verrill 166 78.996 lbs 0.226461
12 Ambrose Pasillas 186 85.000 kg 0.208839
13 Nannette Lehrer 195 93.978 lbs 0.232265
14 Jenifer Mcgarrah 167 94.000 kg 0.316827
15 Tarsha Jarnigan 162 85.806 lbs 0.280547
16 Helen Macgregor 188 101.000 kg 0.288620
17 Kara Belfiore 179 75.818 lbs 0.179407
18 Harvey Quesinberry 168 74.000 kg 0.194019