简体   繁体   English

在有正数和负数的 dataframe 中,如何将正数向上舍入和将负数向下舍入(均为 5 的倍数)?

[英]In a dataframe with both positive & negative numbers, how to round positive numbers up and round negative numbers down (both to multiples of 5)?

With a dataframe as such:对于 dataframe 这样的:

data={'A1':[-7.22,-9.432,-0.00111,0.233,17,23,34],
      'A2':[-23,-7.2455,-0.00222,0.54,1.345,19,21]}
kk=pd.DataFrame(data)
         A1        A2
0  -7.22000 -23.00000
1  -9.43200  -7.24550
2  -0.00111  -0.00222
3   0.23300   0.54000
4  17.00000   1.34500
5  23.00000  19.00000
6  34.00000  21.00000

I want to round positive numbers up but negative numbers down (in multiples of 5), for example:我想向上舍入正数但向下舍入负数(5 的倍数),例如:

  • 21 will become 25 instead of 20 21 将变成 25 而不是 20
  • -7 will become -10 instead of -5 -7 将变为 -10 而不是 -5

Tried using this method only to receive an error尝试使用此方法仅收到错误

def roundupdown5(x):
   if x>=0:
       return np.ceil(x/5)*5
   else:
       return np.floor(x/5)*5

kk[['A1','A2']].apply(lambda x: roundupdown5(x))

Thank you:)谢谢:)

You can use applymap() instead of apply()您可以使用applymap()而不是apply()

kk.applymap(roundupdown5)

results in结果是

      A1    A2
0   -10.0   -25.0
1   -10.0   -10.0
2   -5.0    -5.0
3   5.0 5.0
4   20.0    5.0
5   25.0    20.0
6   35.0    25.0

You can use dict and list comprehension to apply the roundupdown5() function to all the values of data before building the DataFrame :在构建DataFrame之前,您可以使用字典和列表理解将roundupdown5() function 应用于data的所有值:

import pandas as pd
import numpy as np

data={'A1':[-7.22,-9.432,-0.00111,0.233,17,23,34],'A2':[-23,-7.2455,-0.00222,0.54,1.345,19,21]}
kk=pd.DataFrame(data)


def roundupdown5(x):
    if x>=0:
        return np.ceil(x/5)*5
    else:
        return np.floor(x/5)*5

data = {k:[roundupdown5(i) for i in v] for k, v in data.items()}
kk=pd.DataFrame(data)
print(data, kk)

Output: Output:

{'A1': [-10.0, -10.0, -5.0, 5.0, 20.0, 25.0, 35.0], 'A2': [-25.0, -10.0, -5.0, 5.0, 5.0, 20.0, 25.0]}      A1    A2
0 -10.0 -25.0
1 -10.0 -10.0
2  -5.0  -5.0
3   5.0   5.0
4  20.0   5.0
5  25.0  20.0
6  35.0  25.0

You can usenumpy.ceil on the absolute values and bring back the sign withnumpy.sign , then you benefit from vectorial speed:您可以在绝对值上使用numpy.ceil并使用numpy.sign带回符号,然后您将受益于矢量速度:

np.ceil(abs(kk)/5)*5*np.sign(kk)

output: output:

     A1    A2
0 -10.0 -25.0
1 -10.0 -10.0
2  -5.0  -5.0
3   5.0   5.0
4  20.0   5.0
5  25.0  20.0
6  35.0  25.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 dataframe 的列表中同时删除正数和负数? - How to remove both positive and negative numbers from a list in dataframe? 在 Python 中测试两个数字是正数还是负数 - Test if both numbers are positive or negative in Python 如何改善这种检查两个数字都是正数还是负数的模式? - How to improve this pattern of checking if two numbers are both positive or negative? 将 Pandas Dataframe 的列(包含负数和正数)除以特定数字,并在 python 中创建一个新列? - Divide a column(containing both negative numbers and positive numbers) of a Pandas Dataframe with a specific number and create a new column in python? 创建一种从文件中读取正数和负数的方法 - Creating a method to read both positive and negative numbers from a file 从 dataframe 中删除连续的正数/负数 - Remove consecutive positive/negative numbers from a dataframe 用正数创建数据框行,用负数创建其他行 - Create dataframe row with positive numbers and other with negative range() 函数中的负数 - 如何从正数打印到负数? - Negative numbers in range() function - how to print from positive to negative numbers? 如何更改代码以找到负数和正数的立方根的近似值? - How to change the code to find an approximation to the cube root of both negative and positive numbers? 如何根据出现的行总结列表中的负数和正数 - How to sum up negative and positive numbers in a list based on the row they occur
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM