0

I have a folder with 12 .csv files which I wish to merge row by row.

This is the code I have to load one of the .csv

Code

test = pd.read_csv("D:\DAT_ASCII_EURUSD_T_201612.csv", header=None, names=['Date', 'sell_A', 'buy_A', 'unknown'], parse_dates=["Date"])

How would I merge all 12 which have the names shown below (current one is 201612);

DAT_ASCII_EURUSD_T_201601.csv DAT_ASCII_EURUSD_T_201602.csv DAT_ASCII_EURUSD_T_201603.csv DAT_ASCII_EURUSD_T_201604.csv DAT_ASCII_EURUSD_T_201605.csv DAT_ASCII_EURUSD_T_201606.csv DAT_ASCII_EURUSD_T_201607.csv DAT_ASCII_EURUSD_T_201608.csv DAT_ASCII_EURUSD_T_201609.csv DAT_ASCII_EURUSD_T_201610.csv DAT_ASCII_EURUSD_T_201611.csv DAT_ASCII_EURUSD_T_201612.csv

  • you could append of the files together without python, no ? or do you want to do it with python to train yourself ? – MSR974 Aug 07 '20 at 21:31
  • Partially to train myself but if there is a simple way not through python them open to suggestions – InvestingBetter Aug 07 '20 at 21:37
  • https://stackoverflow.com/questions/4969641/how-to-append-one-file-to-another-in-linux-from-the-shell – MSR974 Aug 07 '20 at 21:49
  • on windows: https://stackoverflow.com/questions/19750653/how-to-append-text-files-using-batch-files – MSR974 Aug 07 '20 at 21:50

1 Answers1

3

You can do using concat:

from pathlib import Path

# set your file path
pt = Path("your_file_path/")

name = ['Date', 'sell_A', 'buy_A', 'unknown']
df = pd.concat([pd.read_csv(file, header=None, names=name, parse_dates=["Date"]) for file in pt.glob("*.csv")])
YOLO
  • 20,181
  • 5
  • 20
  • 40