当前位置:   article > 正文

Pandas显示小数位数[pd.round(2)/%.2f]

pd.round

Pandas显示小数位数:pd.round(2)

  1. >>> df = pd.DataFrame(np.random.random([3, 3]),
  2. ... columns=['A', 'B', 'C'], index=['first', 'second', 'third'])
  3. >>> df
  4. A B C
  5. first 0.028208 0.992815 0.173891
  6. second 0.038683 0.645646 0.577595
  7. third 0.877076 0.149370 0.491027
  8. >>> df.round(2)
  9. A B C
  10. first 0.03 0.99 0.17
  11. second 0.04 0.65 0.58
  12. third 0.88 0.15 0.49
  13. >>> df.round({'A': 1, 'C': 2})
  14. A B C
  15. first 0.0 0.992815 0.17
  16. second 0.0 0.645646 0.58
  17. third 0.9 0.149370 0.49
  18. >>> decimals = pd.Series([1, 0, 2], index=['A', 'B', 'C'])
  19. >>> df.round(decimals)
  20. A B C
  21. first 0.0 1 0.17
  22. second 0.0 1 0.58
  23. third 0.9 0 0.49

Python的print中显示小数位数使用占位符:%.2f

  1. # 显示2位小数
  2. a=30.818181818181817
  3. print('%.2f'%a)
  4. Out:30.82

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/726397
推荐阅读
相关标签
  

闽ICP备14008679号