舍入 pandas.DataFrame 的正确方法? [英] The right way to round pandas.DataFrame?

查看:50
本文介绍了舍入 pandas.DataFrame 的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要圆形pandas.DataFrame.

这是我迄今为止尝试过的:

Here is what i have tried so far:

import pandas as pd
data = pd.DataFrame([1.4,2.5,3.8,4.4,5.6],[6.2,7.6,8.8,9.1,0])
print(round(data))

但是当我运行此代码时,出现以下错误:

But when i run this code, i get the following error:

Traceback (most recent call last):
  File "C:\Users\*****\Documents\*****\******\****.py", line 3, in <module>
    print(round(data))
TypeError: type DataFrame doesn't define __round__ method

pandas.DataFrame 的正确方法是什么?

What is the right way to round pandas.DataFrame ?

推荐答案

首先,您可能想要更改数据框的定义,例如:

first, you may want to change the definition of your data frame, something like:

data = pd.DataFrame([[1.4,2.5,3.8,4.4,5.6],[6.2,7.6,8.8,9.1,0]]).T

结果如下:

     0    1
0  1.4  6.2
1  2.5  7.6
2  3.8  8.8
3  4.4  9.1
4  5.6  0.0

或:

data = pd.DataFrame({'A':[1.4,2.5,3.8,4.4,5.6],'B':[6.2,7.6,8.8,9.1,0]})

这样你就得到了两列,否则另一个列表被选为索引;然后:

so that you get two columns, otherwise the other list is picked up as index; then:

data.apply(pd.Series.round)

import numpy as np
data.apply(np.round)

这篇关于舍入 pandas.DataFrame 的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆