首页 > 解决方案 > 熊猫从另一列中的值更新列,但在更新之前操纵值

问题描述

假设我有两个 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"])

但是,由于df1df2具有不同的行数,并且它们的索引(date列)不匹配,因此这是不可行的。

此外,使用df1.update(df2, inplace=True)也不起作用,因为我需要用列avg_temp的平均值更新列min_tempmax_temp

有没有办法在操作和组合列之后更新列?

标签: pythonpandasdataframe

解决方案


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

推荐阅读