类型错误:mean() 得到了一个意外的关键字参数“dtype”#Pandas.DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20430396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: mean() got an unexpected keyword argument 'dtype' # Pandas.DataFrame
提问by Michael
I want to get the numpy.meanof each column of my pandas.DataFrame.
我想获得numpy.mean我的pandas.DataFrame.
Here is my code:
这是我的代码:
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))
If I run this code, I'll get the following error:
如果我运行此代码,我将收到以下错误:
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'
How can I fix that?
我该如何解决?
NOTE:here is an expected output: pd.DataFrame([0.1098537767, -0.0112180033, -0.0205905206])
注意:这是预期的输出:pd.DataFrame([0.1098537767, -0.0112180033, -0.0205905206])
回答by mechanical_meat
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.mean.html
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'>
If you wanted a DataFrame instead of a Series:
如果你想要一个 DataFrame 而不是一个系列:
price_means = pd.DataFrame(prices.mean(axis=0))

