Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将数据帧中的每个数字四舍五入2位小数_Python_Pandas_Rounding - Fatal编程技术网

Python 将数据帧中的每个数字四舍五入2位小数

Python 将数据帧中的每个数字四舍五入2位小数,python,pandas,rounding,Python,Pandas,Rounding,这适用于p_table.apply(pd.Series.round)但是它没有小数点 我尝试了这个p_表。apply(pd.Series.round(2))但是得到了以下错误: unbound method round() must be called with Series instance as first argument (got int instance instead) 如何将数据帧中的所有元素四舍五入到小数点后两位 [编辑]找到了答案 import numpy as np np

这适用于
p_table.apply(pd.Series.round)
但是它没有小数点

我尝试了这个
p_表。apply(pd.Series.round(2))
但是得到了以下错误:

unbound method round() must be called with Series instance as first argument (got int instance instead)
如何将数据帧中的所有元素四舍五入到小数点后两位

[编辑]找到了答案

import numpy as np
np.round(p_table, decimals=2)
即:
data.apply(lambda x:np.round(x,小数=2))
--timeit.timer for 100x:0.00356676544494

相同,但速度较慢,如下所示:
np.round(数据,小数=2)
--timeit.timer for 100x:0.000921095

例如,两者都给出了:

                    x     y     z
Input Sequence                   
1                5.60  0.85 -6.50
2                5.17  0.72 -6.50
3                5.60  0.89 -6.28
4                5.17  0.76 -6.29
有关数据:

                      x       y       z
Input Sequence                         
1                5.6000  0.8519 -6.5000
2                5.1730  0.7151 -6.5000
3                5.6000  0.8919 -6.2794
4                5.1724  0.7551 -6.2888
5                5.6000  0.9316 -6.0587

由于
0.17.0
版本,您可以

要使C列四舍五入,可以使用以下方法:

df['c']=df['c'].apply(lambda x:round(x,2))
输出将是:

        A       B    C
0       t       8    10.96
1       w       2    98.63

下面是一个使用函数执行此操作的可复制示例

# importing pandas as pd 
import pandas as pd 


# generate sample  dataframe  
df = pd.DataFrame(np.random.random([5, 4]), columns =["A", "B", "C"]) 

# use pandas dataframe.round()function to round off all the decimal values to 2 decimal

df.round(2) 

# If you want to customize the round off by individual columns 
df.round({"A":1, "B":2, "C":3}) 

什么是pd?请你给我定义一下好吗?你可以给自己的问题贴一个答案,然后接受它。这个答案帮助我将数值四舍五入到零,因为我需要完全删除小数点。但在结果中,我仍然可以看到。0。那么,您能帮我删除.0s吗?我想这意味着这些值是浮点数,因此您可以根据您对数据帧的要求将其转换为int或string。
        A       B    C
0       t       8    10.958904
1       w       2    98.630137
df['c']=df['c'].apply(lambda x:round(x,2))
        A       B    C
0       t       8    10.96
1       w       2    98.63
# importing pandas as pd 
import pandas as pd 


# generate sample  dataframe  
df = pd.DataFrame(np.random.random([5, 4]), columns =["A", "B", "C"]) 

# use pandas dataframe.round()function to round off all the decimal values to 2 decimal

df.round(2) 

# If you want to customize the round off by individual columns 
df.round({"A":1, "B":2, "C":3})