首页 > 解决方案 > TypeError: 'numpy.float64' object is not callable 在遍历 DataFrame 的 for 循环之后被抛出

问题描述

我有一个 DataFrame 调用data,打印时它看起来像这样:

          open      high       low     close     volume                 date
0     128.5900  129.0000  128.5500  128.8400  1607676.0  2021-01-20 06:30:00
1     128.8300  129.3400  128.7600  129.2219   666924.0  2021-01-20 06:31:00
2     129.2400  129.3000  129.0200  129.0400   518878.0  2021-01-20 06:32:00
3     129.0399  129.1700  128.9300  129.0400   386088.0  2021-01-20 06:33:00
4     129.0300  129.3600  129.0033  129.3400   438125.0  2021-01-20 06:34:00
...        ...       ...       ...       ...        ...                  ...
3895  135.2400  135.2711  134.9800  135.0079   306330.0  2021-02-02 12:55:00
3896  135.0050  135.1500  134.9900  135.1300   248848.0  2021-02-02 12:56:00
3897  135.1300  135.1900  135.0600  135.0800   238290.0  2021-02-02 12:57:00
3898  135.0850  135.0900  134.9200  134.9450   504350.0  2021-02-02 12:58:00
3899  134.9500  135.1900  134.8500  134.9800  1586924.0  2021-02-02 12:59:00

然后我有这个循环,它只遍历我想要的列,以找到平均值。

bar_range_list = []
for num in range(len(data)):
    top = data.loc[num]['high']
    bottom = data.loc[num]['low']
    range = top - bottom
    bar_range_list.append(range)

average_bar = sum(bar_range_list)/ len(bar_range_list)
print(average_bar)

这打印:

0.19887697435897464

然后我有另一个循环:

volume_list = []
for nums in range(len(data)):
    volume = data.loc[nums]['volume']
    volume_list.append(volume)

average_volume = sum(volume_list) / len(volume_list)
print(average_volume)

我希望它只打印整个 DataFrame 的平均体积,但是它打印的是:

for nums in range(len(data)):
TypeError: 'numpy.float64' object is not callable

我不明白为什么当循环与上述循环工作相同并且没有引发错误时会引发此错误。我尝试更改 nums 变量,但没有任何改变。为什么会这样?

标签: python-3.xfor-looptypeerror

解决方案


推荐阅读