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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25272024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:59:28  来源:igfitidea点击:

Round each number in a Python pandas data frame by 2 decimals

pythonpandasrounding

提问by Tristan Forward

This works p_table.apply(pd.Series.round)however it has no decimal places

这有效,p_table.apply(pd.Series.round)但它没有小数位

Documentation says

文档说

import pandas as pd

Series.round(decimals=0, out=None)

i tried this p_table.apply(pd.Series.round(2))but get this error:

我试过这个p_table.apply(pd.Series.round(2))但得到这个错误:

unbound method round() must be called with Series instance as first argument (got int instance instead)

How do I round all elements in the data frame to two decimal places?

如何将数据框中的所有元素四舍五入到两位小数?

[EDIT] Figured it out.

[编辑] 想通了。

import numpy as np
np.round(p_table, decimals=2)

采纳答案by Tristan Forward

import numpy as np
np.round(p_table, decimals=2)

回答by Tristan Forward

that: data.apply(lambda x: np.round(x, decimals=2))--- timeit.timer for 100x: 0.00356676544494

那:data.apply(lambda x: np.round(x, decimals=2))--- timeit.timer for 100x: 0.00356676544494

is same, but slower, as that: np.round(data,decimals=2)--- timeit.timer for 100x: 0.000921095

相同,但速度较慢,因为:np.round(data,decimals=2)--- timeit.timer for 100x: 0.000921095

for example both gives:

例如两者都给出:

                    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

for data:

对于数据:

                      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

回答by piroot

Since 0.17.0version you can do .round(n)

因为0.17.0版本你可以做.round(n)

df.round(2)
      0     1     2     3
0  0.06  0.67  0.77  0.71
1  0.80  0.56  0.97  0.15
2  0.03  0.59  0.11  0.95
3  0.33  0.19  0.46  0.92

df
          0         1         2         3
0  0.057116  0.669422  0.767117  0.708115
1  0.796867  0.557761  0.965837  0.147157
2  0.029647  0.593893  0.114066  0.950810
3  0.325707  0.193619  0.457812  0.920403

回答by Abhinav Vajpeyi

        A       B    C
0       t       8    10.958904
1       w       2    98.630137

To round column C you can use this:

要舍入 C 列,您可以使用以下命令:

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

The output will be:

输出将是:

        A       B    C
0       t       8    10.96
1       w       2    98.63

回答by ewalel

Below is a sample reproducible possible way of doing it using pandasroundfunction.

下面是使用Pandas函数进行此操作的可重复示例示例。

# 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})