Joe
Joe

Reputation: 137

How to load many CSV files in a folder using Python?

I am new to python code and started to notice this pickle function in python. I am trying to load all (50) csv files in a folder and save it as pickle files. The csv files might contain same or different column names too. Any suggestions on how to approach that.

Upvotes: 0

Views: 342

Answers (1)

luigigi
luigigi

Reputation: 4215

You can try something like this:

import glob, os
import pandas as pd
import pickle

os.chdir(r"path/to/folder")
df_list = []
for file in glob.glob("*.csv"):
    df = pd.read_csv(file)
    df_list.append(df)

with open(r'\df_list.pickle', 'wb') as handle:
    pickle.dump(df_list, handle, protocol=pickle.HIGHEST_PROTOCOL)

Upvotes: 1

Related Questions