[英]Problems reading and concatenating CSV files into a single dataframe
I am going to Yahoo Finance and pulling data for German stocks.我要去雅虎财经并提取德国股票的数据。 Then writing them to individual CSV files.
然后将它们写入各个 CSV 文件。
I then want to read them back in to a single dataframe.然后我想将它们读回单个 dataframe。
#Code to get stocks #获取股票的代码
tickers = ["MUV2.DE","DTE.DE", "VNA.DE", "ALV.DE", "BAYN.DE", "EOAN.DE", "RWE.DE", "CON.DE", "HEN3.DE", "BAS.DE", "FME.DE", "WDI.DE", "IFX.DE", "SAP.DE", "BMW.DE", "DPW.DE", "DB1.DE", "DAI.DE", "BEI.DE", "SIE.DE", "ADS.DE", "DBK.DE", "FRE.DE", "HEI.DE", "MRK.DE", "LHA.DE", "VOW3.DE", "1COV.DE", "LIN.DE", "TKA.DE"]
start = datetime.datetime(2012,5,31)
end = datetime.datetime(2020,3,1)
# Go to yahoo and pull data for the following tickers and then write them to CSV
for ticker in tickers:
df = pdr.get_data_yahoo(ticker, start=start, end=end)
df.to_csv(f"{ticker}.csv")
Once the above has been done, I'm reading in a CSV of all the ticker names and then concatenating them with the individual CSV file names.完成上述操作后,我将读取所有代码名称的 CSV,然后将它们与各个 CSV 文件名连接起来。 Well that's what I want to do at least.
好吧,这就是我至少想做的。
import pandas as pd
tickers = pd.read_csv('C:/Users/XXX/Desktop/Momentum/tickers.csv', header=None)[1].tolist()
stocks = (
(pd.concat(
[pd.read_csv(f"C:/Users/XXX/Desktop/Momentum/{ticker}.csv", index_col='Date', parse_dates=True)['Adj Close'].rename(.replace(".DE", "")ticker)
for ticker in tickers],
axis=1,
sort=True)
)
)
stocks = stocks.loc[:,~stocks.columns.duplicated()]
Now I've got this code to work before but when importing other stock tickers.现在我已经让这段代码工作了,但是在导入其他股票代码时。 All my jupyter notebook does is spin out.
我所有的 jupyter notebook 所做的就是旋转出来。
I was wondering what the issue was here and if it was because the CSV file name would be something like ADS.DE.csv and if the first .
我想知道这里的问题是什么,是否是因为 CSV 文件名类似于 ADS.DE.csv,如果第一个
.
is causing issues.正在引起问题。
I solved this problem with the following code.我用下面的代码解决了这个问题。
import os
for filename in os.listdir('dirname'):
os.rename(filename, filename.replace('_intsect_d', ''))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.