代码之家  ›  专栏  ›  技术社区  ›  ViSa

如何在Polars-python中根据条件重命名列?

  •  0
  • ViSa  · 技术社区  · 3 年前

    我正在努力 字段名 基于 条件 在里面 Polars python 但是出现了错误。

    数据:

    import polars as pl
    
    test_df = pl.DataFrame({'Id': [100118647578,
      100023274028,100023274028,100023274028,100118647578,
      100118647578,100118647578,100023274028,100023274028,
      100023274028,100118647578,100118647578,100023274028,
      100118647578,100118647578,100118647578,100118647578,
      100118647578,100118647578,100023274028,100118647578,
      100118647578,100118647578,100118647578,100023274028,
      100118647578,100118647578,100118647578,100023274028,
      100118647578,100118647578,100023274028],
    
     'Age': [49,22,25,18,41,45,42,30,28,
      20,44,56,26,53,40,35,29,
      8,55,23,54,36,52,33,29,
      10,34,39,27,51,19,31],
    
     'Status': [2,1,1,1,1,1,1,3,2,1,1,
      1,2,1,1,1,1,1,1,2,1,1,1,1,2,1,1,
      1,1,1,1,4]})
    

    代码下方 是到 滤器 基于参数值的数据和基于相同基础的重命名:

    def Age_filter(status_filter_value = 1):
        return (
            test_df
            .filter(pl.col('Status') == status_filter_value)
            .sort(['Id','Age'])
            .groupby('Id')
            .agg( pl.col('Age').first())
            .sort('Id')
    
            # below part of code is giving error
            .rename({'Age' : pl.when(status_filter_value == 1)
                                .then('30_DPD_MOB')
                                .otherwise(pl.when(status_filter_value == 2)
                                           .then('60_DPD_MOB')
                                           .otherwise(pl.when(status_filter_value == 3)
                                                      .then('90_DPD_MOB')
                                                      .otherwise('120_DPD_MOB')
                                                      )
                                            )
                    })
        )
    
    Age_filter()
    

    这给出了一个错误: TypeError: argument 'new': 'Expr' object cannot be converted to 'PyString'

    我也试过 低于代码 但这也不起作用:

    def Age_filter1(status_filter_value = 1):
        {
        renamed_value = pl.when(status_filter_value == 1)
                                .then('30')
                                .otherwise(pl.when(status_filter_value == 2)
                                           .then('60')
                                           .otherwise(pl.when(status_filter_value == 3)
                                                      .then('90')
                                                      .otherwise('120')
                                                      )
                                            )
    
    
        return (
            test_df
            .filter(pl.col('Status') == status_filter_value)
            .sort(['Id','Age'])
            .groupby('Id')
            .agg( pl.col('Age').first())
            .sort('Id')
            .rename({'Age' : renamed_value
                    })
        )
        }
    
    Age_filter1()
    
    1 回复  |  直到 3 年前
        1
  •  0
  •   Wayoshi    3 年前

    如错误状态所示 rename 方法只将字符串的dict转换为字符串。不需要复杂的表达式——事实上, pl.when ,等等也应该采用表达式,而不是静态布尔值。

    您可以通过编程方式为您的案例执行以下操作:

    .rename({'Age' : f'{status_filter_value*30}_DPD_MOB')