Python将多个数据帧附加到一个内部函数中
我在收集有关硬币的信息。我需要将所有废弃的数据附加到一个数据帧中 我有以下代码:Python将多个数据帧附加到一个内部函数中,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我在收集有关硬币的信息。我需要将所有废弃的数据附加到一个数据帧中 我有以下代码: import bs4 as bs import urllib.request import pandas as pd def scraping_func(coin_id): """ This function recives "coins_id" from "coins_to_scrape list". It scrapes all data that I need and puts
import bs4 as bs
import urllib.request
import pandas as pd
def scraping_func(coin_id):
"""
This function recives "coins_id" from "coins_to_scrape list".
It scrapes all data that I need and puts it to "df" Dataframe
Then it saves this Dataframe to excel file (%coin_id%.xlsx)
The last lines of the function are: (I cut my code because it's big)
"""
df = df[['Country', 'Value', 'Year', 'Metal', 'Marks', 'Mintage', 'Krause', 'Price', 'Quality', 'Details', 'Avers', 'Revers', 'Gcoins_link']]
excel_name = '{}.xlsx'.format(coin_id)
df.to_excel(excel_name)
for i in dfs:
dfs = df.append(dfs, ignore_index=True)
dfs.to_excel('adfasdf.xlsx')
coins_to_scrape = [514, 515, 179080, 45518, 521, 111429] # The list of ID that I need to scrape
for i in coins_to_scrape: # For each coin in the list
scraping_func(i) # call the "scraping_func" function
它工作得很好。但是这段代码为我传递给函数的每个硬币创建单独的excel文档。但是有数千个硬币需要刮取,所以我想将每个数据帧附加到其中,然后将其保存到excel文件中
我试图在internet上找到解决方案,但我找不到。您可以创建一个数据帧列表,然后将它们连接到单个数据帧中:
def scraping_func(coin_id):
# do stuff to create dataframe
return df[['Country', 'Value', ..., 'Revers', 'Gcoins_link']]
coins_to_scrape = [514, 515, 179080, 45518, 521, 111429]
# construct list of dataframes via list comprehension
df_list = [scraping_func(i) for i in coins_to_scrape]
# combine dataframes in list
df = pd.concat(df_list, ignore_index=True)
你好,jpp。非常感谢你。它对我来说很好用。干杯:)