0

I am trying to merge multiple CSVs using python. The issue I am running into is that each file has a 3 rows of headers. How do I get rid of the 2nd and 3rd row of headers on all the files (just keeping the main header in the 1st row or each file) before merging them?

Thanks!

  • Maybe you should use the `skiprows` parameter of `read_csv` originally. – BigBen Jan 11 '22 at 20:05
  • 1
    Does this answer your question? [Import multiple csv files into pandas and concatenate into one DataFrame](https://stackoverflow.com/questions/20906474/import-multiple-csv-files-into-pandas-and-concatenate-into-one-dataframe) with `skiprows` parameter of course – Chris Jan 11 '22 at 20:06
  • What if I wanted to have all 3 rows of headers to remain at the top of the file? – superEXTmario Jan 11 '22 at 20:29
  • using header = [0,1,2] is causing my date/time column to not be in the 1st column, but in the middle of the file. – superEXTmario Jan 11 '22 at 21:09

1 Answers1

0

try this code.

import pandas as pd
all_filenames = ['f1.csv','f2.csv','f3.csv']
temp=[]
for f in range(len(all_filenames)):
    if f==0:
        temp.append(pd.read_csv(all_filenames[f]))
    else:
        temp.append(pd.read_csv(all_filenames[f]), skiprows=[0])
combined_csv = pd.concat(temp)