简体   繁体   English

Pandas 使用 for 循环连接数据帧

[英]Pandas concatenate dataframes with for loop

I am trying to get tables from a website.我正在尝试从网站获取表格。 The website's URL contains dates so I will have to iterate over dates in order to get historical data.该网站的 URL 包含日期,因此我必须遍历日期才能获取历史数据。 I am generating dates as follows:我生成的日期如下:

import datetime

start = datetime.datetime.strptime("26-09-2016", "%d-%m-%Y")
end = datetime.datetime.strptime("30-09-2016", "%d-%m-%Y")
date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]

dates_list = []
for date in date_generated:
    txt = str(str(date.day) + '.' + str(date.month) + '.' + str(date.year))
    dates_list.append(txt)

dates_list

After this, I am running the code below to concatenate all the tables:在此之后,我运行下面的代码来连接所有表:

for i in range(0, 3):
    allURL = 'https://www.uzse.uz/trade_results?date=' + dates_list[i] + '&locale=en&mkt_id=ALL&page=%d'

    ndf_list = []
    for i in range(1, 100):
        url = allURL %i
        if pd.read_html(url)[0].empty:
            break
        else :
            ndf_list.append(pd.read_html(url)[0])

    ndf = pd.concat(ndf_list)
    ndf.insert(0, 'Date', dates_list[i])

mdf = pd.concat(ndf, ignore_index = True)
mdf

However, this does not work and I get:但是,这不起作用,我得到:

TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"

I do not understand what I am doing wrong.我不明白我做错了什么。 I am expecting to have one table that comes from 26th, 27th, and 28th September.我期待有一张来自 9 月 26 日、27 日和 28 日的桌子。

Please help.请帮忙。

Not sure about the last line(s), but I'd approach it this way不确定最后一行,但我会这样处理

import datetime
import pandas as pd

start = datetime.datetime.strptime("26-09-2016", "%d-%m-%Y")
end = datetime.datetime.strptime("30-09-2016", "%d-%m-%Y")
date_generated = [
    start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]

dates_list = []
for date in date_generated:
    txt = str(str(date.day) + '.' + str(date.month) + '.' + str(date.year))
    dates_list.append(txt)

dates_list

ndf = pd.DataFrame()  # create empty ndf
for i in range(0, 3):
    allURL = 'https://www.uzse.uz/trade_results?date=' + \
        dates_list[i] + '&locale=en&mkt_id=ALL&page=%d'

    # ndf_list = []
    for j in range(1, 100):
        url = allURL % j
        if pd.read_html(url)[0].empty:
            break
        else:
            # ndf_list.append(pd.read_html(url)[0])
            chunk = pd.read_html(url)[0]
            chunk['Date'] = dates_list[i] # Date is positioned at last position, let's fix that
            # get a list of all the columns
            cols = chunk.columns.tolist()
            # rearrange the columns, move the last element (Date) to the first position
            cols = cols[-1:] + cols[:-1]
            # reorder the dataframe
            chunk = chunk[cols]
            ndf = pd.concat([ndf, chunk])

    # ndf = pd.concat(ndf_list)

# ndf.insert(0, 'Date', dates_list[i])

print(ndf)
# mdf = pd.concat(ndf, ignore_index=True)
# mdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM