使用Pandas逐行有条件地求和并舍入到最近的偶数值

inkz8wg9  于 2023-05-12  发布在  其他
关注(0)|答案(4)|浏览(121)

我希望将一行值的总和四舍五入到最接近的偶数,除非该值在0.75和3之间,则该值应四舍五入为2。

数据

Location    range   type    Q1 24   Q2 24   Q3 24   Q4 24   
NY          low     SS      0       0.79    0       0   
NY          low     SS      0       0       0       0   
CA          low     AA      0.24    0.34    0.93    1.07    
CA          low     AA      1.71    0.57    0       0   
CA          low     BB      0.08    0.11    0.65    0.47    
CA          low     BB      0       0       2       0   
CA          low     DD      0.35    0.5     1.37    1.57

期望

Location     range    type    Q1 24   Q2 24   Q3 24   Q4 24    rounded_sum
    NY          low     SS      0       0.79    0       0        2
    NY          low     SS      0       0       0       0        0
    CA          low     AA      0.24    0.34    0.93    1.07     2 
    CA          low     AA      1.71    0.57    0       0        2
    CA          low     BB      0.08    0.11    0.65    0.47     2  
    CA          low     BB      0       0       2       0        2
    CA          low     DD      0.35    0.5     1.37    1.57     4

SO成员有帮助,但如果总和大于或等于0.75,则需要调整,它应该四舍五入到值2。

# using np.where, check if integer part is even or odd
# if odd, add 1 else, keep the integer as is
df2['rounded_sum']=np.where(df2['rounded_sum'].astype(int)%2==1,
                           df2['rounded_sum'].astype(int) +1, 
                           df2['rounded_sum'].astype(int))

任何建议都很感激

woobm2wo

woobm2wo1#

似乎你可以定义一个函数来舍入你的数字

def func(n):
    if 0.75 <= n <= 3:
        return 2
    elif n % 2 < 1:
        return n - n % 2
    else:
        return n - n % 2 + 2

然后形成一个新的数组,并用Q1、Q2、Q3和Q4的和填充它:

sums = df.iloc[:, 3:].sum(axis=1)

然后你可以用“map”和我们的函数修改你的数组,并把它写到你的dataframe中:

df["rounded"] = list(map(func, sums))

或者将其替换为lambda:

df["rounded"] = list(map(lambda n: 2 if 0.75 <= n <= 3 else n - n % 2 if n % 2 < 1 else n - n % 2 + 2, sums))

但我认为没有lambda的代码看起来更好)
P.S.对不起我的英语

xlpyo6sf

xlpyo6sf2#

np.where中使用round

df["sum"] = df[["Q1 24", "Q2 24", "Q3 24", "Q4 24"]].sum(axis=1)
df["rounded_sum"] = np.where(df["sum"].between(0.75,3),2,round(df["sum"].div(2))*2)

>>>
  Location range type  Q1 24  Q2 24  Q3 24  Q4 24   sum  rounded_sum
0       NY   low   SS   0.00   0.79   0.00   0.00  0.79          2.0
1       NY   low   SS   0.00   0.00   0.00   0.00  0.00          0.0
2       CA   low   AA   0.24   0.34   0.93   1.07  2.58          2.0
3       CA   low   AA   1.71   0.57   0.00   0.00  2.28          2.0
4       CA   low   BB   0.08   0.11   0.65   0.47  1.31          2.0
5       CA   low   BB   0.00   0.00   2.00   0.00  2.00          2.0
6       CA   low   DD   0.35   0.50   1.37   1.57  3.79          4.0
xu3bshqb

xu3bshqb3#

让我们试试np.where

s = df.filter(like='Q').sum(axis=1)
df['rounded_sum'] = np.where(s.between(0.75,2), 2, (s/2).round()*2)

注意.between(0.75, 2).between(0.75,3)由于round()操作而给予相同的结果。

ki0zmccv

ki0zmccv4#

试试看

# sum the rows from column "type" onwards
df['sum'] = df.iloc[:, 3:].sum(axis=1)

# round the sum according to the requirements
df['rounded_sum'] = np.where((df['sum'] >= 0.75) & (df['sum'] <= 2.99), 2, np.round(df['sum'] / 2) * 2)

相关问题