如何通过循环将csv的负载导入不同的python数据帧?

时间:2018-12-04 14:57:54

标签: python loops csv

我有很多csv文件。我想创建一个循环,让我可以这样做;

    df_20180731 = pd.read_csv('path/cust_20180731.csv')

大约36个文件中的每个文件。

我的文件是df_20160131,df_20160231 ...... df_20181231等。基本上是在月底之前。

谢谢

3 个答案:

答案 0 :(得分:2)

continue

答案 1 :(得分:2)

您可以执行以下操作:

Mammal

答案 2 :(得分:1)

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