Drop all CSV files under any directory into DataFrame

If you have a large number of CSV files that you want to drop into DataFrame You can define a DataFrame with the variable name "filename_df" by using the following command.

import os
import pandas as pd

 for dirname, _, filenames in os.walk (directory name):
    for filename in filenames:
        path = os.path.join(dirname, filename)
 Dynamically define the file name as a variable name using the #exec command
        exec("{0}_df = pd.read_csv(path)".format(filename.replace('.csv','')))

Recommended Posts

Drop all CSV files under any directory into DataFrame
Delete all pyc files under the specified directory
Unzip all zip files under the current directory
Run all unittests under a directory
[For beginners] Read Excel / CSV files into DataFrame with Google Colaboratory
List all files under the current directory line by line with full path
Read all csv files in the folder