I am trying to combine multiple CSVs in a folder then delete the first 3 columns. My code combines the files ok but I can't drop the columns. Can anyone see the issue?
import pandas as pd
import glob
path = r'C:\Users\****' # use your path
all_files = glob.glob(path + "/*.csv")
li = []
for filename in all_files:
df = pd.read_csv(filename, index_col=None, header=0)
li.append(df)
frame = pd.concat(li, axis=0, ignore_index=True)
frame.drop(frame.columns[[0, 1, 2]], axis=1)
print(frame)
frame.to_csv('out.csv', index=False)
You can use . iloc[:,3]
and select the columns index you desire:
from pathlib import Path
import pandas as pd
path = Path('C:\Users\****') # use your path
all_files = path.glob("*.csv")
frame = pd.concat(
(pd.read_csv(file_, index_col=None, header=0).iloc[:,3:]
for file_ in all_files), axis=0, ignore_index=True)
frame.to_csv('out.csv', index=False)