python - 熊猫从另一列中的值更新列,但在更新之前操纵值
问题描述
假设我有两个 DataFrame:
df1:
avg_temp total_precipitation
date
2020-03-01 5.8 0.2
2020-03-02 3.4 0
2020-03-03 4.8 0
2020-03-04 2.2 0
2020-03-05 1.4 0
2020-03-06 3.7 0
2020-03-07 7 0
2020-03-08 9.3 0
2020-03-09 NaN NaN
2020-03-10 NaN NaN
2020-03-11 NaN NaN
2020-03-12 NaN NaN
2020-03-13 NaN NaN
2020-03-14 3.6 0
2020-03-15 NaN NaN
2020-03-16 NaN NaN
和df2:
min_temp max_temp precipitation_probability
date
2020-03-15 8.0 8.0 0.24
2020-03-16 -2.0 9.0 0.16
我需要执行以下操作:如果有任何date
列匹配的行,请将数据框的列avg_temp
替换df1
为(df2["min_temp"]+df2["max_temp"])/2
来自df2
.
我尝试了以下代码:
df1["avg_temp"] = np.where(df1["date"] == df2["date"], (df2["min_temp"]+df2["max_temp"])/2, df1["avg_temp"])
但是,由于df1
和df2
具有不同的行数,并且它们的索引(date
列)不匹配,因此这是不可行的。
此外,使用df1.update(df2, inplace=True)
也不起作用,因为我需要用列avg_temp
的平均值更新列min_temp
和max_temp
有没有办法在操作和组合列之后更新列?
解决方案
DataFrame.update
与一些预处理一起使用- 添加了新的平均值列和rename
列:
df22 = (df2.assign(avg_temp = (df2["min_temp"]+df2["max_temp"])/2)
.rename(columns={'precipitation_probability':'total_precipitation'}))
df1.update(df22)
print (df1)
avg_temp total_precipitation
date
2020-03-01 5.8 0.20
2020-03-02 3.4 0.00
2020-03-03 4.8 0.00
2020-03-04 2.2 0.00
2020-03-05 1.4 0.00
2020-03-06 3.7 0.00
2020-03-07 7.0 0.00
2020-03-08 9.3 0.00
2020-03-09 NaN NaN
2020-03-10 NaN NaN
2020-03-11 NaN NaN
2020-03-12 NaN NaN
2020-03-13 NaN NaN
2020-03-14 3.6 0.00
2020-03-15 8.0 0.24
2020-03-16 3.5 0.16
推荐阅读
- reactjs - 在 springboot 邮件 API 中启用 CORS
- css - 边距自动没有居中弹性项目
- oracle - 用于返回结果集的 where 子句中的 case 语句包含空值
- javascript - 将三元运算符转换为 if
- c# - 是否可以使用 IEnumerable 为类中的字段创建一个 foreach 循环?
- python - 如何通过 Rstudio 在 python 脚本中正确导入熊猫
- php - SQL 突发问题 - 一般错误:1364 字段没有默认值
- javascript - 根据其他两个输入更改文本字段的输入值
- java - 调用另一个类时递归不起作用
- vb.net - 无法从 Windows 7 电脑使用 Visual Basic 发送电子邮件:“操作已超时”