将 Pandas 数据框中的值转换为两位小数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46336878/
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-09-14 04:30:03 来源:igfitidea点击:
Convert values in pandas dataframe to two decimal points
提问by jezrael
I have a pandas dataframe as follows.
我有一个Pandas数据框如下。
NO Topic A Topic B Topic C
0 0.0 1.000 1.000 1.000
1 1.0 0.550 0.640 0.550
2 2.0 0.567 0.740 0.675
3 3.0 0.850 0.860 0.850
4 4.0 0.200 0.200 0.200
5 5.0 0.850 0.840 0.850
6 6.0 0.450 0.533 0.450
7 7.0 0.625 0.657 0.700
8 8.0 0.575 0.500 0.575
9 9.0 0.850 0.900 0.880
10 10.0 0.950 0.971 0.960
Now I want to update the entire dataframe in a way it contains values of two decimal ponits. e.g., 0.625 -> 0.63 etc.
现在我想以包含两个小数点值的方式更新整个数据帧。例如,0.625 -> 0.63 等。
Is it possible to do it in pandas?
有可能在Pandas中做到吗?
回答by jezrael
It seems you need DataFrame.round
:
看来你需要DataFrame.round
:
df = df.round(2)
print (df)
NO Topic A Topic B Topic C
0 0.0 1.00 1.00 1.00
1 1.0 0.55 0.64 0.55
2 2.0 0.57 0.74 0.68
3 3.0 0.85 0.86 0.85
4 4.0 0.20 0.20 0.20
5 5.0 0.85 0.84 0.85
6 6.0 0.45 0.53 0.45
7 7.0 0.62 0.66 0.70
8 8.0 0.57 0.50 0.57
9 9.0 0.85 0.90 0.88
10 10.0 0.95 0.97 0.96