【问题标题】:TypeError: 'method' object is not subscriptable for pandas dataframeTypeError:“方法”对象不可为熊猫数据框下标
【发布时间】:2021-03-04 13:33:03
【问题描述】:

此程序显示错误以获取 rollingmean 输出。

    dff = pd.Series([ 0.001215,0.001571,0.002944, 0.005101, 0.007357, 0.009988, 0.013653, 0.017936, 0.021971, 0.025590 ])
    dff = pd.DataFrame(dff, columns=['A'])
    avg = dff['A'].rolling(3).mean()
    avg = [x*5 for x in avg] 
    dff['mean'] = avg
    
    listpos = 0 #We use a counter to move over the different data columns
    
    for datapoint in dff.A:
        rollingmean = dff.mean[listpos] 

模拟后给出如下错误信息:

TypeError                                 Traceback (most recent call last)
<ipython-input-114-61e9145fea85> in <module>()
     13 for datapoint in dff.A:
---> 14     rollingmean = dff.mean[listpos] #Get local mean

TypeError: 'method' object is not subscriptable

这段代码有什么问题?

【问题讨论】:

    标签: python pandas dataframe csv for-loop


    【解决方案1】:

    dff.mean 更改为dff["mean"]。该错误是由于名称冲突导致dff.mean被识别为库内置方法pd.DataFrame.mean()

    【讨论】:

    • 好的。谢谢。
    猜你喜欢
    • 2018-07-13
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2018-04-27
    • 1970-01-01
    • 2019-01-18
    相关资源
    最近更新 更多