简体   繁体   中英

Import multiple csv files using loops

I'm using google colab and I'm trying to import multiple csv files from google drive to the program.

I know how to import the datasets one by one but I'm not sure how to create a loop that reads in all of the csv files so that I can just have one line of code that imports all of the datasets for me.

You can create a dictionary with all dataframes like this:

from glob import glob
import pandas as pd

filepaths = glob('/content/drive/My Drive/location_of_the_files/*.csv')
dfs = {f'df{n}': pd.read_csv(i) for n, i in enumerate(filepaths)}

Individual dataframes can then be accessed with dfs['df0'] , dfs['df1'] , etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM