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!

4
  • Maybe you should use the skiprows parameter of read_csv originally. Commented Jan 11, 2022 at 20:05
  • 1
    Does this answer your question? Import multiple csv files into pandas and concatenate into one DataFrame with skiprows parameter of course Commented Jan 11, 2022 at 20:06
  • What if I wanted to have all 3 rows of headers to remain at the top of the file? Commented Jan 11, 2022 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. Commented Jan 11, 2022 at 21:09

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.