指定在 reversed(row) 函数中查询的列数

Specify number of columns consulted in reversed(row) function

上下文

我的 df 类似于:

df_a = pd.DataFrame({
    '2020_Q1': [2, 3, 6, 20, 20],
    '2020_Q2': [2, 3, 6, 20, 20],
    '2020_Q3': [5, 3, 6, 20, 20], 
    '2020_Q4': [5, 4, 7, 20, 20],
    '2021_Q1': [5, 3, 7, 20, 20],
    '2021_Q2': [5, 4, 0, 20, 20],
}, index = ['People', 'AA', 'BB', 'MM', '$$'])
df_a.columns = pd.to_datetime(df_a.columns).to_period('Q')
df_a

Out[1]: 
          2020Q1   2020Q2   2020Q3  2020Q4   2021Q1   2021Q2
People        2        2        5        5        5        5
AA            3        3        3        4        3        4
BB            6        6        6        7        7        0
MM           20       20       20       20       20       20
$$           20       20       20       20       20       20

以及一个根据每一行的计算方式设置年末值的函数。行 df['People', 'AA', 'BB'] 按一年最后一个季度计算,值高于零,如果 none 更高则为零,财务值 df['MM', '$$'] 计算为 SUM一年中的季度

def compute_end_year_value(row):
    if row.name in ['People', 'AA', 'BB']:
        for val in reversed(row):
            if val > 0:
                return val
        return 0
    return sum(row)

问题

但是,如果我 运行 按原样运行函数,它将查询整行。我如何在定义中添加一种方法来指定反向函数以仅考虑插入它的最后 n 列?

所以当我执行类似以下代码的操作时,我得到了输出

df_a.insert(4,'2020_Total', df_a.apply(compute_end_year_value, axis = 1, steps = 4))
df_a.insert(7,'2021_Total', df_a.apply(compute_end_year_value, axis = 1, steps = 2))
df_a 

Out[1]: 
         2020Q1   2020Q2   2020Q3   2020Q4   2020_Total   2021Q1  2021Q2  2021_Total
People        2        2        5        5           5        5        5           5
AA            3        3        3        4           4        3        4           4
BB            6        6        6        6           6        7        0           7
MM           20       20       20       20          80       20       20          40
$$           20       20       20       20          80       20       20          40
           

您正在迭代应用函数中的行而不是列。

要获得所需的结果,您需要将轴指定为列。

df_a.insert(4,'2020_Total', df_a.apply(lambda col: compute_end_year_value(col), axis=1))
df_a.insert(7,'2021_Total', df_a.apply(lambda col: compute_end_year_value(col), axis=1))

>>>
        2020_Q1  2020_Q2  2020_Q3  2020_Q4  2020_Total  2021_Q1  2021_Q2  \
People        2        2        5        5           5        5        5   
AA            3        3        3        4           4        3        4   
BB            6        6        6        7           7        7        0   
MM           20       20       20       20         120       20       20   
$$           20       20       20       20         120       20       20   

        2021_Total  
People           5  
AA               4  
BB               7  
MM             240  
$$             240  

有关应用函数的详细信息,请参阅 docs

编辑

为了处理每年,我认为最简单的方法是告诉函数要考虑哪一年。我将 compute_end_year_value 方法修改为以下内容:

def compute_end_year_value(row,year):
    col_pos_year = [col.startswith(year) for col in df_a.columns]
    valid_year_columns = df_a.columns[col_pos_year]
    row = row[valid_year_columns]
    if row.name in ['People', 'AA', 'BB']:
          for val in reversed(row):
              if val > 0:
                  return val
          return 0
    
    return sum(row)

df_a.insert(4,'2020_Total', df_a.apply(lambda col: compute_end_year_value(col, "2020"), axis=1))
df_a.insert(7,'2021_Total', df_a.apply(lambda col: compute_end_year_value(col, "2021"), axis=1))
df_a
>>>
        2020_Q1  2020_Q2  2020_Q3  2020_Q4  2020_Total  2021_Q1  2021_Q2  \
People        2        2        5        5           5        5        5   
AA            3        3        3        4           4        3        4   
BB            6        6        6        7           7        7        0   
MM           20       20       20       20          80       20       20   
$$           20       20       20       20          80       20       20   

        2021_Total  
People           5  
AA               4  
BB               7  
MM              40  
$$              40  

我添加了一个列表,用于检查该列是否以特定年份开头并仅选择要迭代的那些列。