如何用python从一个文件中按时间顺序导入所有csv文件?

ycl3bljg  于 2023-05-11  发布在  Python
关注(0)|答案(5)|浏览(129)

我的文件夹中有大约2000个CSV文件。我想按时间顺序读一读。他们用数字命名,所以我想这一定很容易。

我用下面的代码来阅读它们。我可以想象一个非常简单的解决方案,因为必须有一个简单的参数。但我没有找到任何东西:((

def csv_to_df():
    dff_all_from_csv = []
    
    for root, dirs, files in os.walk("output/csv_files"):
        for file in files:
            df = pd.read_csv(os.path.join(root, file))
            dff_all_from_csv.append(df)
    return dff_all_from_csv

我真的很感激你的帮助!谢谢

gkl3eglg

gkl3eglg1#

您可以split * filename* 并使用词干/数字作为sortingkey

def csv_to_df():
    dff_all_from_csv = []
    
    for root, dirs, files in os.walk("output/csv_files"):
        for file in sorted(files, key=lambda x: int(x.split(".")[0])): # <- line updated
            df = pd.read_csv(os.path.join(root, file))
            dff_all_from_csv.append(df)
    return dff_all_from_csv

或者从natsort使用natsorted

#pip install natsort
from natsort import natsorted

    ...
    for root, dirs, files in os.walk("output/csv_files"):
        for file in natsorted(files): # <- line updated
        ...
egmofgnx

egmofgnx2#

您可以尝试:

column_df = pd.read_csv(r'1.csv')
column_df.columns

all_csv_df = pd.DataFrame(columns=column_df.columns)
for i in range(1,5):
    r = pd.read_csv(r''+str(i)+'.csv')
    all_csv_df = all_csv_df.append(r)

    
all_csv_df
dsekswqp

dsekswqp3#

您可以使用pathliblstat属性按创建时间(st_ctime)或修改时间(st_mtime)对文件进行排序:

import pathlib

DATA_DIR = 'output/csv_files'

dff_all_from_csv = [pd.read_csv(f) for f in sorted(DATA_DIR.glob('*.csv'),
                                                   key=lambda x: x.lstat().st_mtime)]
epggiuax

epggiuax4#

您可以使用os.path.getmtime()检索csv文件的日期。您可以将创建日期添加到可排序的列表中。然后你可以从排序列表中打开 Dataframe 。

import os
import time
import pandas as pd

path_to_csv_files = "./csv_files/"

# list in which we'll store the name and the last modification date of each csv file
metadata = list()

for _, _, files in os.walk("./csv_files"):
   for name in files:
      # retrieving the last modif date and formating it so it is is numerically sortable
      creation_date = time.strftime("%Y%m%d%H%M%S",time.gmtime(os.path.getmtime(f"{path_to_csv_files}{name}")))
      # turing it into an int so we can sort the metadata per date
      creation_date = int(creation_date)
      metadata.append((name, creation_date))

# sorting the metadata per date
metadata = sorted(
    metadata, 
    key=lambda x: x[1]
    )

# list of dataframes placed in date order
list_of_df_from_csv = list()

for name, _ in metadata:
   path_to_csv = path_to_csv_files+name
   df = pd.read_csv(path_to_csv)
   list_of_df_from_csv.append(df)
fhity93d

fhity93d5#

我尝试了这样的东西,它工作得很好:

import os
import pandas as pd

def csv_to_df():
    
    folder_path = "output/csv_files"
    
    files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith('.csv')]
    files = sorted(files, key=os.path.getmtime)
    
    dff_all_from_csv = []
    for file in files:
        df = pd.read_csv(file)
        dff_all_from_csv.append(df)
    
    return dff_all_from_csv

相关问题