首页 > 解决方案 > 如何在 pandas Dataframe 中对不同月份应用不同的条件?

问题描述

所以我试图应用不同的条件,这些条件取决于日期,具体的月份。例如,对于 1 月,替换 TEMP 中高于 45 的数据,但对于 2 月,替换高于 30 的数据,依此类推。我已经用下面的代码做到了,但问题是上个月的数据是用 nan 替换的。

这是我的代码:

meses = ["01", "02"]

for i in var_vars:
    if i in dataframes2.columns.values:
        for j in range(len(meses)):
            test_prueba_mes = dataframes2[i].loc[dataframes2['fecha'].dt.month == int(meses[j])]
            test_prueba = test_prueba_mes[dataframes2[i]<dataframes.loc[i]["X"+meses[j]+".max"]]
            dataframes2["Prueba " + str(i)] = test_prueba

输出:

    dataframes2.tail(5)

 fecha  TEMP_C_Avg  RH_Avg  Prueba TEMP_C_Avg  Prueba RH_Avg
21 2020-01-01 22:00:00        46.0     103                NaN            NaN
22 2020-01-01 23:00:00        29.0     103                NaN            NaN
23 2020-01-02 00:00:00        31.0       3                NaN            NaN
24 2020-01-02 12:00:00        31.0       2                NaN            NaN
25 2020-02-01 10:00:00        29.0       5               29.0            5.0

我想要的输出是:

输出:

fecha  TEMP_C_Avg  RH_Avg  Prueba TEMP_C_Avg  Prueba RH_Avg
21 2020-01-01 22:00:00        46.0     103                NaN            NaN
22 2020-01-01 23:00:00        29.0     103               29.0            NaN
23 2020-01-02 00:00:00        31.0       3               31.0            3.0
24 2020-01-02 12:00:00        31.0       2               31.0            2.0
25 2020-02-01 10:00:00        29.0       5               29.0            5.0

感谢是否有人可以帮助我。

更新:6 个月的规则集是 1 月 45 日、2 月 30 日、3 月 45 日、10 日、5 月 15 日、6 月 30 日

数据示例:

                 fecha  TEMP_C_Avg  RH_Avg
25 2020-02-01 10:00:00        29.0       5
26 2020-02-01 11:00:00        32.0     105
27 2020-03-01 10:00:00        55.0       3
28 2020-03-01 11:00:00        40.0       5
29 2020-04-01 10:00:00        10.0      20
30 2020-04-01 11:00:00         5.0      15
31 2020-05-01 10:00:00        20.0      15
32 2020-05-01 11:00:00         5.0     106
33 2020-06-01 10:00:00        33.0     107
34 2020-06-01 11:00:00        20.0      20

标签: pythonpandasdate

解决方案


有清晰的认识

  • 已将每月限制编码为dict 限制
  • 使用 numpy select(),当条件匹配时,从第二个参数中获取与条件相对应的值。默认为第三个参数
  • 从限制动态构建条件 dict
  • 第二个参数需要与条件相同的长度list。建立理解列表,np.nan使其list长度正确
  • 要考虑所有列,请使用构建参数的dict理解**kwargassign()
df = pd.read_csv(io.StringIO("""                 fecha  TEMP_C_Avg  RH_Avg
25  2020-02-01 10:00:00        29.0       5
26  2020-02-01 11:00:00        32.0     105
27  2020-03-01 10:00:00        55.0       3
28  2020-03-01 11:00:00        40.0       5
29  2020-04-01 10:00:00        10.0      20
30  2020-04-01 11:00:00         5.0      15
31  2020-05-01 10:00:00        20.0      15
32  2020-05-01 11:00:00         5.0     106
33  2020-06-01 10:00:00        33.0     107
34  2020-06-01 11:00:00        20.0      20"""), sep="\s\s+", engine="python")
df.fecha = pd.to_datetime(df.fecha)

# The ruleset for 6 months is jan 45, feb 30, mar 45, abr 10, may 15, jun 30
limits = {1:45, 2:30, 3:45, 4:10, 5:15, 6:30}

df = df.assign(**{f"Prueba {c}":np.select( # construct target column name
        # build a condition for each of the month limits
        [df.fecha.dt.month.eq(m) & df[c].gt(l) for m,l in limits.items()], 
        [np.nan for m in limits.keys()], # NaN if beyond limit
        df[c]) # keep value if within limits
            for c in df.columns if "Avg" in c}) # do calc for all columns that have "Avg" in name

费查 TEMP_C_Avg RH_Avg 普鲁巴 TEMP_C_Avg Prueba RH_Avg
25 2020-02-01 10:00:00 29 5 29 5
26 2020-02-01 11:00:00 32 105
27 2020-03-01 10:00:00 55 3 3
28 2020-03-01 11:00:00 40 5 40 5
29 2020-04-01 10:00:00 10 20 10
30 2020-04-01 11:00:00 5 15 5
31 2020-05-01 10:00:00 20 15 15
32 2020-05-01 11:00:00 5 106 5
33 2020-06-01 10:00:00 33 107
34 2020-06-01 11:00:00 20 20 20 20

推荐阅读