简体   繁体   中英

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.

I then want to read them back in to a single 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. 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.

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 . 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', ''))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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