Khutso Mphelo
Khutso Mphelo

Reputation: 63

Rename csv columns in a folder of multiple csv

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,

Upvotes: 1

Views: 790

Answers (2)

Jiajia Zheng
Jiajia Zheng

Reputation: 26

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

Upvotes: 0

Mo7art
Mo7art

Reputation: 303

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"]

Upvotes: 1

Related Questions