【问题标题】:How to iterate thru a dataframe sending batches of 1000 records at a time until it reaches the total 30K records如何遍历一次发送 1000 条记录的数据帧,直到达到总共 30K 条记录
【发布时间】:2021-10-09 21:49:54
【问题描述】:

我有一个 30K 记录数据框,我通过 API 调用传递它以获取数据验证。一次调用所有 30K 会破坏 Python 内核;有没有办法让 for 循环一次循环遍历我的整个数据帧 1000 条记录?

这就是我提取 1000 条记录的方式:

df1, errors = extract_data(df=data1, limit=1000, timeout=60)
df1 

extract_data 是一个函数,我可以在其中限制发送到 API 调用的记录,在本例中将其限制为 1K。

但我想要这样的东西:

LIMIT = 1000
for index in data1.iterrows():
    df1, errors = extract_data(df=data1, limit=1000, timeout=60)
    if LIMIT == index, break

 

【问题讨论】:

    标签: python for-loop iterator batching


    【解决方案1】:

    iloc是你的朋友:

    for index in range(0, 30):
        df.iloc[index * 1000: (index + 1) * 1000]
    

    【讨论】:

    • 谢谢@Cor - 但我怎样才能在 for 循环中传递 'df1, errors = extract_data(df=data1, limit=1000, timeout=60)'?
    • extract_data(df=df.iloc[index * 1000: (index + 1) * 1000], limit=1000, timeout=60) 工作吗?
    猜你喜欢
    • 1970-01-01
    • 2010-11-02
    • 2020-01-13
    • 2019-09-30
    • 2014-06-22
    • 1970-01-01
    • 2020-12-11
    • 2020-05-10
    • 2017-10-31
    相关资源
    最近更新 更多