I have a directory full of txt files about apple stocks like this:
Date,Open,High,Low,Close,Volume,Adj Close 2012-10-15,632.35,635.13,623.85,634.76,15446500,631.87
I need to get the names of all files that have the format APPL_*.txt, load all files of those names in my Notebook, and concatenate them together to form one data frame of 61 rows and 7 columns. I hope I was clear enough, thank you in advance!
Use Pandas
import pandas as pd
import os
os.chdir('directory')
# loop through files
collector = []
for i in os.listdir():
if 'AAPL_' in i and i.endswith('.txt'):
txt_df = pd.read_csv(i, delimiter = ',')
collector.append(txt_df)
# combine the list of dataframes from your text files
df = pd.concat(collector)
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.