TypeError:mean()得到一个意外的关键字参数'dtype'#Pandas.DataFrame

时间:2013-12-06 17:52:49

标签: python numpy pandas

我希望获得numpy.mean的每一列的pandas.DataFrame

这是我的代码:

import pandas as pd
import numpy as np

prices = pd.DataFrame([[-0.33333333, -0.25343423, -0.1666666667],
                       [+0.23432323, +0.14285714, -0.0769230769],
                       [+0.42857143, +0.07692308, +0.1818181818]])

print(np.mean(prices, axis=0))

如果我运行此代码,我将收到以下错误:

Traceback (most recent call last):
  File "C:\Users\*****\Documents\Python\******\****.py", line 8, in <module>
    print(np.mean(new, axis=0))
  File "C:\Python33\lib\site-packages\numpy\core\fromnumeric.py", line 2711, in mean
    return mean(axis=axis, dtype=dtype, out=out)
TypeError: mean() got an unexpected keyword argument 'dtype'

我该如何解决?

注意:这是预期的输出:pd.DataFrame([0.1098537767, -0.0112180033, -0.0205905206])

1 个答案:

答案 0 :(得分:4)

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.mean.html

>>> prices.mean(axis=0)
0    0.109854
1   -0.011218
2   -0.020591
dtype: float64
>>> type(prices.mean(axis=0))
<class 'pandas.core.series.Series'>

如果您想要一个DataFrame而不是系列:

price_means = pd.DataFrame(prices.mean(axis=0))