根据条件更新多列熊猫

时间:2020-07-07 03:53:23

标签: python-3.x

我有一个数据框。我想基于少数条件一次更新多个列。我尝试了以下操作,但是它在某行上失败并给出错误“密钥必须具有相同的长度”:

     df[["col1","col2","col4","col4"]] = df.apply(lambda x:self.function(x),axis = 1)
              
     def function(x):
                if ((x["last_price_update_dttm"] > x["update_time_price_stream"])
                | (pd.isnull(x["update_time_price_stream"]) == True)):
                    return x[["col1","col2","col4","col4"]]
                else:
                    return x[["col5","col6","col7","col8"]]

我也尝试过np.where,但是形状不匹配时出现错误:

            df[["col1","col2","col4","col4"]] = np.where(
            (
                (df["last_price_update_dttm"] > df["update_time_price_stream"])
                | (pd.isnull(df["update_time_price_stream"]) == True)
            ),
            df[[["col1","col2","col4","col4"]]],
            df[["col5","col6","col7","col8"]]
        )

有什么建议吗?

0 个答案:

没有答案