简体   繁体   English

使用 pandas.cut 时,有没有办法对结果进行四舍五入?

[英]Is there a way to round your results when using pandas.cut?

I am trying to take receive data, bin the data, and then output the data.我正在尝试接收数据,将数据分箱,然后 output 数据。 I am doing so using pandas.cut.我正在使用 pandas.cut 这样做。 I am looking to alter my code so the bounds produced by pandas.cut are whole numbers.我希望更改我的代码,因此 pandas.cut 生成的边界是整数。 Below is my current code:以下是我当前的代码:

for (ColumnName, columnData) in df.iteritems():
    df[ColumnName+'_binned']=pd.cut(df[ColumnName],number_of_bins))
    #cutting each row into bins 

I would like the bins to only have whole numbers instead of decimals.我希望这些垃圾箱只有整数而不是小数。 For example: the current output is 1.75-5.33.例如:当前output为1.75-5.33。 I would like this result to be rounded to 2 - 6. What is the best way to alter my current code?我希望将此结果四舍五入为 2 - 6。更改当前代码的最佳方法是什么? Is this possible?这可能吗? Thank you in advance.先感谢您。

This is the following code I used to fix this previously answered question:这是我用来解决这个先前回答的问题的以下代码:

for (ColumnName, columnData) in df.iteritems():
    df[ColumnName+'_binned']=pd.cut(df[ColumnName],bin_count, precision=0)

Setting the precision to zero rounded all the answers.将精度设置为零会使所有答案四舍五入。 From the previously issued problem, it returns answers such as 2.0-6.0.从先前发布的问题中,它返回诸如 2.0-6.0 之类的答案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM