I have about 200 CSV files in a folder and some of the columns are not named the same for instance there is 'App name' ,'App Name' and 'app name' so I want to rename the this kind of columns to a standard name like 'App Name' and concatenate all the CSV's to one CSV file,
Asked
Active
Viewed 706 times
1
-
3loop over files. use pandas to read. concat dataframes. set column names – luigigi Feb 14 '20 at 08:54
-
Is the amount of columns and their order the same? – bartcode Feb 14 '20 at 08:55
-
The amount of columns is the same, I just do not know how to loop through it – Khutso Mphelo Feb 14 '20 at 08:57
-
and yes the order is the same – Khutso Mphelo Feb 14 '20 at 09:09
-
check out [this](https://stackoverflow.com/questions/20906474/import-multiple-csv-files-into-pandas-and-concatenate-into-one-dataframe) – luigigi Feb 14 '20 at 09:27
2 Answers
1
That would work:
import glob
import os
import pandas as pd
folder = 'the folder path'
filenames = [i for i in glob.glob(folder + os.path.sep + '*.csv')]
combined_csv = pd.concat([pd.read_csv(f, skiprows=1) for f in filenames])
combined_csv.columns = ["all the header labels"]

Mo7art
- 173
- 8
0
import glob
import pandas as pd
csv_folder = '/Users/yourname/folder_containing_csv/*.csv'
csv_file_list = []
for csv_path in glob.glob(csv_folder):
csv_file_list.append(csv_path)
for i in range(len(csv_file_list)):
df = pd.read_csv(csv_file_list[i], index_col=0)
df = df.rename(columns={"old_name": "new_name"})
file_name = str(csv_file_list[i]).split('.csv')[0].split('/')[-1:][0] # get file name by subsetting the path
df.to_csv('/Users/yourname/%(file_name)s.csv'%{'file_name': file_name}) # save to .csv using the same file name

Jiajia Zheng
- 26
- 5