使用:
newdf3.pivot_table(rows=['Quradate'],aggfunc=np.mean)
产生:
Alabama_exp Credit_exp Inventory_exp National_exp Price_exp Sales_exp
Quradate
2010-01-15 0.568003 0.404481 0.488601 0.483097 0.431211 0.570755
2010-04-15 0.543620 0.385417 0.455078 0.468750 0.408203 0.564453
我想将小数四舍五入为两位数并乘以 100,例如 .568003 应该是 57 已经摆弄了一段时间但无济于事;试过这个
newdf3.pivot_table(rows=['Quradate'],aggfunc=np.mean).apply(round(2)) #and got:
TypeError: ("'float' object is not callable", u'occurred at index Alabama_exp')
尝试了很多其他方法都无济于事大多数人提示该项目不是 float 的...我看到 Pandas 系列对象有一个 round 方法但 DF 没有我尝试使用 df.apply 但它提示 float 问题。
最佳答案
只需使用numpy.round
,例如:
100 * np.round(newdf3.pivot_table(rows=['Quradate'], aggfunc=np.mean), 2)
只要 round 适用于所有列类型,这适用于 DataFrame
。
一些数据:
In [9]: dfrm
Out[9]:
A B C
0 -1.312700 0.760710 1.044006
1 -0.792521 -0.076913 0.087334
2 -0.557738 0.982031 1.365357
3 1.013947 0.345896 -0.356652
4 1.278278 -0.195477 0.550492
5 0.116599 -0.670163 -1.290245
6 -1.808143 -0.818014 0.713614
7 0.233726 0.634349 0.561103
8 2.344671 -2.331232 -0.759296
9 -1.658047 1.756503 -0.996620
In [10]: 100*np.round(dfrm, 2)
Out[10]:
A B C
0 -131 76 104
1 -79 -8 9
2 -56 98 137
3 101 35 -36
4 128 -20 55
5 12 -67 -129
6 -181 -82 71
7 23 63 56
8 234 -233 -76
9 -166 176 -100
关于python - Pandas DataFrame 中的舍入条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19100540/