如何通过循环将CSV负载导入不同的python数据帧?
我有很多csv文件。我想创建一个允许我这样做的循环如何通过循环将CSV负载导入不同的python数据帧?,python,loops,csv,Python,Loops,Csv,我有很多csv文件。我想创建一个允许我这样做的循环 df_20180731 = pd.read_csv('path/cust_20180731.csv') 大约36个文件中的每一个 我的档案是df_20160131,df_20160231。。。。。。df_20181231等基本上在月底前确定日期 谢谢您可以这样做: import glob import pandas as pd datasets = {} for file in glob.glob('path/df_*'):
df_20180731 = pd.read_csv('path/cust_20180731.csv')
大约36个文件中的每一个
我的档案是df_20160131,df_20160231。。。。。。df_20181231等基本上在月底前确定日期
谢谢您可以这样做:
import glob
import pandas as pd
datasets = {}
for file in glob.glob('path/df_*'):
datasets[file] = pd.read_csv(file)
你可以这样做:
import glob
import pandas as pd
datasets = {}
for file in glob.glob('path/df_*'):
datasets[file] = pd.read_csv(file)
os.listdirpath生成列表,然后迭代获取数据帧列表。listdirpath生成列表,然后迭代获取数据帧列表
import os
import pandas as pd
# get a list of all the files in the directory
files = os.listdir(<path of the directory containing all the files>)
#iterate over all the files and store it in a dictionary
dataframe = {file: pd.read_csv(file) for file in files}
#if the directory must contain other files,
#you can check the file paths with any logic(extension etc.), in that case
def logic(fname):
return '.csv' in fname
dataframe = {file: pd.read_csv(file) for file in files if logic(file) }
#this will create a dictionary of file : dataframe_objects
I hope it helps