我要下载许多.csv文件,这些文件会转换为pandas数据帧并相互附加。
可以通过每天创建的url来访问csv,并且可以使用datetime轻松地将其生成并放在列表中。
我可以在列表中单独打开它们。
当我尝试打开其中一些并将它们附加在一起时,我得到一个空的数据框。代码看起来像这样。
#Imports
import datetime
import pandas as pd
#Testing can open .csv file
data = pd.read_csv('https://promo.betfair.com/betfairsp/prices/dwbfpricesukwin01022018.csv')
data.iloc[:5]
#Taking heading to use to create new dataframe
data_headings = list(data.columns.values)
#Setting up string for url
path_start = 'https://promo.betfair.com/betfairsp/prices/dwbfpricesukwin'
file = ".csv"
#Getting dates which are used in url
start = datetime.datetime.strptime("01-02-2018", "%d-%m-%Y")
end = datetime.datetime.strptime("04-02-2018", "%d-%m-%Y")
date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]
#Creating new dataframe which is appended to
for heading in data_headings:
data = {heading: []}
df = pd.DataFrame(data, columns=data_headings)
#Creating list of url
date_list = []
for date in date_generated:
date_string = date.strftime("%d%m%Y")
x = path_start + date_string + file
date_list.append(x)
#Opening and appending csv files from list which contains url
for full_path in date_list:
data_link = pd.read_csv(full_path)
df.append(data_link)
print(df)
我已经检查了它们不仅是空的csv,而且不是。任何帮助将不胜感激。
干杯, 桑迪
答案 0 :(得分:1)
您永远不会存储附加的数据帧。该行:
df.append(data_link)
应该是
df = df.append(data_link)
但是,这可能是错误的方法。您确实想使用URL数组并将它们连接起来。查看this similar question,看看它是否可以改善您的代码!
答案 1 :(得分:0)
我真的不明白你想在这里做什么:
#Creating new dataframe which is appended to
for heading in data_headings:
data = {heading: []}
df = pd.DataFrame(data, columns=data_headings)
顺便尝试一下:
for full_path in date_list:
data_link = pd.read_csv(full_path)
df.append(data_link.copy())