简体   繁体   English

根据来自另一个 DataFrame 的值更新 pandas 列中的值

[英]Update values in pandas columns based on values from another DataFrame

Two DataFrames df1 and df2 both has Weight in lb columns if Weight in lb matches I need to update the corresponding BMI in df1 from df2.如果 lb 中的权重匹配,则两个 DataFrame df1df2都在 lb 列中具有权重,我需要从 df2 更新 df1 中的相应 BMI。

DataFrame df1 =数据帧df1 =

Index指数 First Name Age年龄 Gender性别 Weight in lb重量(磅) BMI体重指数
0 0 James詹姆士 21 21 Male男性 167 167
1 1 John约翰 25 25 Male男性 175 175
2 2 Patricia帕特里夏 23 23 Female女性 132 132
4 4 Kevin凯文 22 22 Male男性 169 169
5 5 Alex亚历克斯 27 27 Male男性 169 169

DataFrame df2 =数据帧df2 =

Weight in lb重量(磅) BMI体重指数 Height高度
165 165 16.5 16.5 180 180
166 166 17.0 17.0 180 180
167 167 17.3 17.3 180 180
168 168 17.4 17.4 180 180
169 169 17.9 17.9 180 180
170 170 18.4 18.4 180 180
171 171 18.7 18.7 180 180
172 172 18.9 18.9 180 180
173 173 19.2 19.2 180 180
174 174 19.3 19.3 180 180
175 175 19.6 19.6 180 180
176 176 19.9 19.9 180 180
177 177 20.0 20.0 180 180
178 178 20.2 20.2 180 180
179 179 21.6 21.6 180 180
180 180 21.9 21.9 180 180
181 181 22.1 22.1 180 180
182 182 22.3 22.3 180 180
183 183 22.5 22.5 180 180
184 184 22.8 22.8 180 180
185 185 22.9 22.9 180 180
186 186 23.0 23.0 180 180
187 187 22.1 22.1 180 180
188 188 22.1 22.1 180 180

I tried我试过了

df1['BMI'] = df2.loc[df2['Weight in lb'].isin(df1['Weight in lb'],'BMI']

But it is not working, I cannot do merge operation because the original dataframe is too complex for that但它不起作用,我无法进行合并操作,因为原始数据框太复杂了

Desired Output df1 =所需输出df1 =

Index指数 First Name Age年龄 Gender性别 Weight in lb重量(磅) BMI体重指数
0 0 James詹姆士 21 21 Male男性 167 167 17.3 17.3
1 1 John约翰 25 25 Male男性 175 175 19.6 19.6
2 2 Patricia帕特里夏 23 23 Female女性 132 132
4 4 Kevin凯文 22 22 Male男性 169 169 17.9 17.9
5 5 Alex亚历克斯 27 27 Male男性 169 169 17.9 17.9

You can use df2 to make a mapping from weight to BMI and then use it on df1您可以使用 df2 进行从体重到 BMI 的映射,然后在 df1 上使用它

weight_to_bmi = dict(df2.values)
df1['BMI'] = df1['Weight in lb'].map(weight_to_bmi)

Building off of mitoRibo's Answer建立在 mitoRibo 的答案之上

weight_to_bmi = pd.Series(df2['Weight in lb'].values,index=df2['BMI']).to_dict()
df1['BMI'] = df1['Weight in lb'].map(weight_to_bmi)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 子集根据另一个数据帧的值在多个列上进行pandas数据帧 - Subset pandas dataframe on multiple columns based on values from another dataframe 根据来自另一个数据框的数据将值分配给Pandas数据框中的列 - Assign values to columns in Pandas Dataframe based on data from another dataframe Pandas 根据另一个数据框中 2 列的值过滤行 - Pandas filter rows based on values from 2 columns in another dataframe 使用来自另一个数据框的值更新熊猫数据框 - Update pandas dataframe with values from another dataframe 根据来自另一个数据帧的值更新数据帧 - Update a dataframe based on values from another dataframe Pandas:使用基于两列的另一个数据帧中的值替换一个数据帧中的值 - Pandas: replace values in one dataframe with values from another dataframe based on two columns 从 pandas dataframe 中选择值,基于另一个 dataframe 中具有最小值/最大值的列 - Selecting values from pandas dataframe based off of columns with min/max values in another dataframe 如何根据另一列的值更改 Pandas DataFrame 中的值 - How to change values in a Pandas DataFrame based on values of another columns 将值添加到基于另一个 dataframe 的 pandas dataframe 列 - adding values to pandas dataframe columns based on another dataframe 根据另一个数据帧将列添加到 Pandas 数据帧并将值设置为零 - Add columns to Pandas dataframe based on another dataframe and set values to zero
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM