Pandas round不适用于DataFrame

时间:2016-07-07 21:45:06

标签: python pandas dataframe rounding

Round适用于单个元素但不适用于DataFrame,尝试使用DataFrame.round()但不起作用......任何想法?感谢。

请输入以下代码:

print "Panda Version: ", pd.__version__
print "['5am'][0]: ", x3['5am'][0]
print "Round element: ", np.round(x3['5am'][0]*4) /4
print "Round Dataframe: \r\n", np.round(x3 * 4, decimals=2) / 4
df = np.round(x3 * 4, decimals=2) / 4
print "Round Dataframe Again: \r\n", df.round(2)

得到了结果:

Panda Version:  0.18.0
['5am'][0]:  0.279914529915
Round element:  0.25
Round Dataframe:
                 5am       6am      7am      8am      9am     10am     11am
Date
2016-07-11  0.279915  0.279915  2.85256  4.52778  6.23291  9.01496  8.53632
2016-07-12  0.339744  0.369658  2.67308  4.52778  5.00641  7.30983  6.98077
2016-07-13  0.399573  0.459402  2.61325  3.83974  5.48504  6.77137  5.24573
2016-07-14  0.339744  0.549145  2.64316  3.36111  5.66453  5.96368  7.87821
2016-07-15  0.309829  0.459402  2.55342  4.64744  4.46795  6.80128  6.17308
2016-07-16      0.25  0.369658  2.46368  2.67308  4.58761  6.35256  5.63462
2016-07-17  0.279915  0.369658  2.58333  2.91239  4.19872  5.51496  6.65171
Round Dataframe Again:
                 5am       6am      7am      8am      9am     10am     11am
Date
2016-07-11  0.279915  0.279915  2.85256  4.52778  6.23291  9.01496  8.53632
2016-07-12  0.339744  0.369658  2.67308  4.52778  5.00641  7.30983  6.98077
2016-07-13  0.399573  0.459402  2.61325  3.83974  5.48504  6.77137  5.24573
2016-07-14  0.339744  0.549145  2.64316  3.36111  5.66453  5.96368  7.87821
2016-07-15  0.309829  0.459402  2.55342  4.64744  4.46795  6.80128  6.17308
2016-07-16      0.25  0.369658  2.46368  2.67308  4.58761  6.35256  5.63462
2016-07-17  0.279915  0.369658  2.58333  2.91239  4.19872  5.51496  6.65171

4 个答案:

答案 0 :(得分:11)

尝试强制转换为浮点类型:

x3.astype(float).round(2)

答案 1 :(得分:3)

就这么简单

df['col_name'] = df['col_name'].astype(float).round(2)

答案 2 :(得分:1)

您的代码说明:

In [166]: np.round(df * 4, decimals=2)
Out[166]:
      a     b     c     d
0  0.11  0.45  1.65  3.38
1  3.97  2.90  1.89  3.42
2  1.46  0.79  3.00  1.44
3  3.48  2.33  0.81  1.02
4  1.03  0.65  1.94  2.92
5  1.88  2.21  0.59  0.39
6  0.08  2.09  4.00  1.02
7  2.86  0.71  3.56  0.57
8  1.23  1.38  3.47  0.03
9  3.09  1.10  1.12  3.31

In [167]: np.round(df * 4, decimals=2) / 4
Out[167]:
        a       b       c       d
0  0.0275  0.1125  0.4125  0.8450
1  0.9925  0.7250  0.4725  0.8550
2  0.3650  0.1975  0.7500  0.3600
3  0.8700  0.5825  0.2025  0.2550
4  0.2575  0.1625  0.4850  0.7300
5  0.4700  0.5525  0.1475  0.0975
6  0.0200  0.5225  1.0000  0.2550
7  0.7150  0.1775  0.8900  0.1425
8  0.3075  0.3450  0.8675  0.0075
9  0.7725  0.2750  0.2800  0.8275

In [168]: np.round(np.round(df * 4, decimals=2) / 4, 2)
Out[168]:
      a     b     c     d
0  0.03  0.11  0.41  0.84
1  0.99  0.72  0.47  0.86
2  0.36  0.20  0.75  0.36
3  0.87  0.58  0.20  0.26
4  0.26  0.16  0.48  0.73
5  0.47  0.55  0.15  0.10
6  0.02  0.52  1.00  0.26
7  0.72  0.18  0.89  0.14
8  0.31  0.34  0.87  0.01
9  0.77  0.28  0.28  0.83

这对我来说很合适(pandas 0.18.1)

In [162]: df = pd.DataFrame(np.random.rand(10,4), columns=list('abcd'))

In [163]: df
Out[163]:
          a         b         c         d
0  0.028700  0.112959  0.412192  0.845663
1  0.991907  0.725550  0.472020  0.856240
2  0.365117  0.197468  0.750554  0.360272
3  0.870041  0.582081  0.203692  0.255915
4  0.257433  0.161543  0.483978  0.730548
5  0.470767  0.553341  0.146612  0.096358
6  0.020052  0.522482  0.999089  0.254312
7  0.714934  0.178061  0.889703  0.143701
8  0.308284  0.344552  0.868151  0.007825
9  0.771984  0.274245  0.280431  0.827999

In [164]: df.round(2)
Out[164]:
      a     b     c     d
0  0.03  0.11  0.41  0.85
1  0.99  0.73  0.47  0.86
2  0.37  0.20  0.75  0.36
3  0.87  0.58  0.20  0.26
4  0.26  0.16  0.48  0.73
5  0.47  0.55  0.15  0.10
6  0.02  0.52  1.00  0.25
7  0.71  0.18  0.89  0.14
8  0.31  0.34  0.87  0.01
9  0.77  0.27  0.28  0.83

答案 3 :(得分:0)

我试图重现你的情况。它看起来效果很好。

import pandas as pd
import numpy as np

from io import StringIO

s = """Date 5am       6am      7am      8am      9am     10am     11am
2016-07-11  0.279915  0.279915  2.85256  4.52778  6.23291  9.01496  8.53632
2016-07-12  0.339744  0.369658  2.67308  4.52778  5.00641  7.30983  6.98077
2016-07-13  0.399573  0.459402  2.61325  3.83974  5.48504  6.77137  5.24573
2016-07-14  0.339744  0.549145  2.64316  3.36111  5.66453  5.96368  7.87821
2016-07-15  0.309829  0.459402  2.55342  4.64744  4.46795  6.80128  6.17308
2016-07-16      0.25  0.369658  2.46368  2.67308  4.58761  6.35256  5.63462
2016-07-17  0.279915  0.369658  2.58333  2.91239  4.19872  5.51496  6.65171
"""

df = pd.read_table(StringIO(s), delim_whitespace=True)
df.set_index('Date').round(2)