如何使用特定小数点舍入 pandas 数据框中的所有值
我正在尝试舍入 pandas 数据框中的小数点,我尝试使用 df.round(4), 如果值为 1.23456,则它会转换为 1.2346,但我期待 1.2345,请查找下面的代码和我的虚拟数据框、从 df.round() 函数获得的输出和预期输出。
import pandas as pd
df1 = pd.DataFrame({"Col1":[2.45623786, 5.752739399,77.9488494,66.7477474733,929999.883838978], "Col2":["A","B","C","D","F"],"Col3":["A1","A2","A3","A4","A5"], "Col4":["2022-01-01","2022-01-02 12:33:45","2022/12/12","20221123","2022/12/23"],"Col5":[12,233,6464,775,86543334]})
print(df1.head())
df2 = df1.round(6)
print(df2.head)
实际数据
df1.round(3) 函数的输出
预期输出为
提前致谢...... ...
I am trying to round decimal points in pandas Data Frame, I tried with df.round(4),
if the value is 1.23456 it's converting 1.2346, but I am expecting 1.2345, Please find below code and my dummy data frame, output I got from df.round() function and expected output.
import pandas as pd
df1 = pd.DataFrame({"Col1":[2.45623786, 5.752739399,77.9488494,66.7477474733,929999.883838978], "Col2":["A","B","C","D","F"],"Col3":["A1","A2","A3","A4","A5"], "Col4":["2022-01-01","2022-01-02 12:33:45","2022/12/12","20221123","2022/12/23"],"Col5":[12,233,6464,775,86543334]})
print(df1.head())
df2 = df1.round(6)
print(df2.head)
Actual Data
Output from df1.round(3) function
Expected output would be
Thanks in Advance........
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要的话,您可以尝试使用 math.floor() 函数对数字进行四舍五入。
You can try using math.floor() function which round down the number, if this is what you want.