【问题标题】:How to create many data frames and combine them in one big data frame to avoid creating multiple variables如何创建多个数据框并将它们组合在一个大数据框中以避免创建多个变量
【发布时间】:2021-03-14 00:44:34
【问题描述】:

我正在抓取 HTML 并将数据存储在 pandas 数据框中。我需要一个循环,因为 html 中的数据位于多个 url 中。我的第一个想法是创建与 url 一样多的数据帧,创建许多变量,但我读过这是一个坏主意。我读过的解决方案是创建一个字典,但我不知道如何使用数据帧来做到这一点。我只想要一个最终数据帧,其中包含从第一个数据帧的第一行到最后一个数据帧的最后一行的信息。

这是我目前的代码

# To simulate I am a browser and send request to get the body of the response.
header = {
  "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36",
  "X-Requested-With": "XMLHttpRequest"
}
# Coingecko containes 100 coins per page
uncomplete_URL = "https://www.coingecko.com/en?page="
for page in  range (1,100): # Because I only want to analise the first 1000 coins
    complete_URL = uncomplete_URL + str(page)
    # To request info and then populate a pandas dataframe
    r = requests.get(complete_URL, headers=header)
    dfs = pd.read_html(r.text)
    df = pd.concat(df) # EDIT I forgot this line
    tmp_df.head()

大部分代码取自here

【问题讨论】:

  • 我会将数据帧存储在一个列表中,然后在其中执行pd.concat(dfs)

标签: python dataframe


【解决方案1】:

通常,我会将所有数据框放在字典中,因为它更快更高效。完成循环后,我使用 pd concat

dict_df = {}
for page in  range (1,100): # Because I only want to analise the first 1000 coins
    complete_URL = uncomplete_URL + str(page)
    # To request info and then populate a pandas dataframe
    r = requests.get(complete_URL, headers=header)
    dfs = pd.read_html(r.text)
    dict_df[page] = dfs
final_df = pd.concat(dict_df.values(), ignore_index=True)

【讨论】:

    【解决方案2】:

    你可以用pd.concat这样做,

    d = pd.DataFrame() # init a dataframe
    for page in  range (1,2): # Because I only want to analise the first 1000 coins
        complete_URL = uncomplete_URL + str(page)
        # To request info and then populate a pandas dataframe
        r = requests.get(complete_URL, headers=header)
        dfs = pd.read_html(r.text)
        tmp_df_str(page) = pd.concat(dfs)
        tmp_df.head()
        d = pd.concat([d, tmp_df]) # concat `tmp_df` with initial dataframe `d`
    
    print(d)
    

    【讨论】:

    • 我正在尝试应用您的建议,但出现此错误:TypeError: cannot concatenate object of type '';只有 Series 和 DataFrame obj 是有效的
    • 你的dfs是什么类型的?不是数据框吗? tmp_df 是什么?
    • 我的错。我忘记了我将添加注释的一行代码。
    • 总是阳光明媚,现在我忘记了最后一行,你的建议奏效了。非常感谢
    • 我看到我只有 100 行,为什么?
    【解决方案3】:

    这是我在应用了回答的人的建议后得到的最终结果

    # To simulate I am a browser and send request to get the body of the response.
    header = {
      "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36",
      "X-Requested-With": "XMLHttpRequest"
    }
    d = pd.DataFrame() # init a dataframe
    # Coingecko containes 100 coins per page
    uncomplete_URL = "https://www.coingecko.com/en?page="
    for page in  range (1,3): # Because I only want to analise the first 1000 coins
        complete_URL = uncomplete_URL + str(page)
        # To request info and then populate a pandas dataframe
        r = requests.get(complete_URL, headers=header)
        dfs = pd.read_html(r.text)
        tmp_df = pd.concat(dfs)
        d = pd.concat([d, tmp_df]) # concat `tmp_df` with initial dataframe `d`
    
    d 
    
    

    【讨论】:

      猜你喜欢
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 2019-09-22
      • 2012-11-25
      相关资源
      最近更新 更多