python 如何在循环中使用'iloc'?

pn9klfpd  于 2022-12-28  发布在  Python
关注(0)|答案(1)|浏览(229)

'

def start():
    ob = client.futures_order_book(symbol = 'BTCUSDT')
    ob = pd.DataFrame(ob)
    b = ob['bids']
    a = ob['asks']
    n = 1
    for n in range(500):
        b = b.iloc[n]
        print(b)
        n = n+1

'
返回错误AttributeError: 'list' object has no attribute 'iloc'
它在循环外对(')有效

n = 1
b = b.iloc[n]
print(b)

`)
可以。但是我需要从数据框中读取500个字符串的数据。我该怎么修复它呢?

nhaq1z21

nhaq1z211#

在你的例子中,你将b设置为ob['bids'],但是在你的for循环中,你将b设置为b.iloc[n]。因此你应该看到至少有一个值被打印出来。在第二轮循环中,b不再是ob['bids'],所以你得到了错误
您似乎正在尝试打印bids列的内容。您可以使用以下命令轻松完成此操作:

print(list(ob['bids']))

下面是一个例子:

代码:

import pandas as pd
import random

# Creating a dataframe for illustration purposes
ob = pd.DataFrame({"bids":[random.randint(1,30) for i in range(500)],
                   "asks":[random.randint(1,35) for i in range(500)]})

print(list(ob['bids']))
输出:
[28, 18, 11, 20, 29, 25, 11, 13, 18, 13, 7, 27, 13, 2, 27, 17, 8, 24, 10, 3, 13, 6, 15, 3, 2, 21, 21, 2, 22, 17, 26, 1, 13, 23, 17, 17, 2, 23, 6, 27, 21, 23, 7, 10, 25, 3, 20, 4, 3, 5, 21, 8, 21, 19, 13, 6, 1, 28, 4, 27, 7, 19, 26, 27, 20, 25, 27, 10, 9, 8, 3, 1, 6, 24, 28, 2, 15, 9, 23, 25, 23, 13, 15, 29, 3, 6, 5, 6, 1, 12, 10, 10, 6, 8, 17, 3, 14, 28, 9, 26, 4, 21, 3, 2, 27, 10, 28, 23, 23, 9, 5, 7, 28, 25, 8, 5, 2, 10, 5, 11, 23, 8, 12, 2, 15, 9, 16, 28, 8, 21, 12, 4, 20, 21, 14, 22, 24, 25, 20, 12, 28, 11, 19, 25, 27, 5, 17, 8, 15, 12, 22, 2, 19, 4, 24, 23, 25, 5, 27, 25, 28, 22, 3, 21, 16, 9, 5, 14, 3, 20, 15, 21, 8, 16, 15, 29, 30, 22, 9, 21, 19, 20, 30, 4, 16, 26, 22, 28, 12, 2, 23, 9, 10, 18, 18, 11, 6, 4, 23, 4, 19, 15, 4, 30, 30, 2, 13, 3, 14, 13, 27, 17, 22, 15, 26, 22, 4, 5, 13, 7, 4, 16, 18, 4, 15, 25, 4, 26, 14, 10, 3, 26, 29, 9, 22, 24, 4, 22, 5, 23, 29, 6, 25, 24, 16, 4, 26, 9, 10, 25, 16, 23, 8, 28, 16, 26, 25, 24, 17, 13, 13, 2, 18, 4, 23, 2, 5, 21, 2, 17, 19, 4, 3, 4, 29, 19, 20, 23, 23, 27, 28, 2, 2, 23, 17, 18, 3, 25, 30, 29, 9, 8, 13, 24, 19, 5, 7, 20, 19, 15, 18, 1, 2, 22, 23, 22, 5, 29, 19, 14, 27, 12, 3, 24, 16, 29, 17, 10, 29, 20, 26, 14, 18, 25, 23, 7, 22, 21, 7, 19, 12, 10, 9, 17, 2, 4, 3, 2, 13, 25, 20, 18, 30, 20, 21, 11, 9, 21, 18, 29, 4, 7, 23, 28, 18, 17, 15, 30, 3, 19, 4, 2, 19, 5, 27, 19, 22, 12, 14, 17, 9, 8, 20, 17, 20, 9, 26, 20, 18, 6, 12, 10, 8, 4, 6, 14, 20, 10, 2, 22, 17, 2, 28, 8, 5, 4, 4, 26, 9, 21, 27, 12, 27, 12, 6, 11, 29, 27, 29, 22, 27, 10, 24, 22, 22, 8, 30, 13, 10, 22, 23, 26, 14, 6, 6, 20, 14, 4, 6, 27, 12, 9, 14, 9, 29, 8, 14, 30, 2, 28, 24, 14, 15, 6, 17, 18, 25, 15, 7, 6, 28, 30, 30, 10, 15, 24, 15, 27, 4, 20, 12, 15, 18, 1, 11, 24, 21, 24, 20, 18, 17, 10, 22, 22, 23, 23, 1, 11, 30, 15, 21, 4, 23, 8, 11, 29, 25, 5, 30, 19, 1, 4, 6, 25, 25, 23, 4, 6, 11, 17]

注:

不太清楚为什么要在for循环中递增n,因为这应该是自动发生的,除非你试图打印每一个秒值?
在我的示例中,您可以像这样打印每隔一个数字:

print(list(ob['bids'])[::2])
输出:
[28, 11, 29, 11, 18, 7, 13, 27, 8, 10, 13, 15, 2, 21, 22, 26, 13, 17, 2, 6, 21, 7, 25, 20, 3, 21, 21, 13, 1, 4, 7, 26, 20, 27, 9, 3, 6, 28, 15, 23, 23, 15, 3, 5, 1, 10, 6, 17, 14, 9, 4, 3, 27, 28, 23, 5, 28, 8, 2, 5, 23, 12, 15, 16, 8, 12, 20, 14, 24, 20, 28, 19, 27, 17, 15, 22, 19, 24, 25, 27, 28, 3, 16, 5, 3, 15, 8, 15, 30, 9, 19, 30, 16, 22, 12, 23, 10, 18, 6, 23, 19, 4, 30, 13, 14, 27, 22, 26, 4, 13, 4, 18, 15, 4, 14, 3, 29, 22, 4, 5, 29, 25, 16, 26, 10, 16, 8, 16, 25, 17, 13, 18, 23, 5, 2, 19, 3, 29, 20, 23, 28, 2, 17, 3, 30, 9, 13, 19, 7, 19, 18, 2, 23, 5, 19, 27, 3, 16, 17, 29, 26, 18, 23, 22, 7, 12, 9, 2, 3, 13, 20, 30, 21, 9, 18, 4, 23, 18, 15, 3, 4, 19, 27, 22, 14, 9, 20, 20, 26, 18, 12, 8, 6, 20, 2, 17, 28, 5, 4, 9, 27, 27, 6, 29, 29, 27, 24, 22, 30, 10, 23, 14, 6, 14, 6, 12, 14, 29, 14, 2, 24, 15, 17, 25, 7, 28, 30, 15, 15, 4, 12, 18, 11, 21, 20, 17, 22, 23, 1, 30, 21, 23, 11, 25, 30, 1, 6, 25, 4, 11]

相关问题