gpt4 book ai didi

python - TypeError : mean() got an unexpected keyword argument 'dtype' # Pandas. 数据帧

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:28 29 4
gpt4 key购买 nike

我想获取 pandas.DataFrame 每一列的 numpy.mean

这是我的代码:

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])

最佳答案

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 而不是 Series:

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

关于python - TypeError : mean() got an unexpected keyword argument 'dtype' # Pandas. 数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20430396/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com