[英]How to update the data frame column values from another data frame based a conditional match in pandas
[英]How do I update the values of a column in a data frame based on the values of another data frame in Python?
df1
| location | latitude
0 | NaN | 41.0
1 | NaN | 43.0
2 | NaN | 72.0
3 | NaN | 74.0
df2
| location | min_latitude | max_latitude
0 | point_a | 40.0 | 45.0
1 | point_b | 70.0 | 75.0
如何根据latitude
介于min_latitude
和max_latitude
之间的条件更新df1
的location
值,就像下面的结果一样?
df1
| location | latitude
0 | point_a | 41.0
1 | point_a | 43.0
2 | point_b | 72.0
3 | point_b | 74.0
我试过使用df1['location'].apply(lambda x: df2['location'] if df1['location'].between(df2['min_latitude'], df2['max_latitude']) else df1['location'])
,但它返回ValueError: Can only compare identically-labeled Series objects
。
您不需要使用 between,您可以使用布尔索引并获得您想要的:
df1["location"][(df1["latitude"] > df2["min_lattitude"]["point_a"]) & (df1["latitude"] < df2["max_lattitude"]["point_a"])] = "point_a"
和 point_b 类似的东西。 你也可以通过遍历一个点列表来做很多点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.