简体   繁体   中英

I would like to combine multiple csv files from my google drive to pandas

This is what I have so far. I need to combine 3 files from my google drive to one. I do not get an error with this code, but it only imports 1 file.

import pandas as pd 
import glob 

path = '/content/gdrive/My Drive/Colab Datasets/'
all_files = glob.glob(path + "/*.csv") # this is new

li = []

for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0)
    li.append(df)

frame = pd.concat(li, axis=0, ignore_index=True)

Simply by following this code:

import os
import glob
import pandas as pd
os.chdir("content/gdrive/My Drive/Colab Datasets")


extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]


#combine all files in the list
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames])

#export to csv
combined_csv.to_csv( "combined_csv.csv", index=False, encoding='utf-8-sig')

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