Skip to content Skip to sidebar Skip to footer

Get File Created Date - Add To Dataframes Column On Read_csv

I need to pull many (hundreds) CSV's into a pandas dataframe. I need to a add the date the file was created in a column upon read in to the pandas dataframe for each CSV file. I ca

Solution 1:

if the different files have the same columns and you would like to append different files into rows.

import pandas as pd
import time
import os

# lis of files you want to read
files = ['one.csv', 'two.csv']

column_names = ['c_1', 'c_2', 'c_3']

all_dataframes = []
for file_name in files:
    df_temp = pd.read_csv(file_name, delimiter=',', header=None)
    df_temp.columns = column_names
    df_temp['creation_time'] = time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime(file_name)))
    df_temp['file_name'] = file_name
    all_dataframes.append(df_temp)

df = pd.concat(all_dataframes, axis=0, ignore_index=True)

df

output:

enter image description here

if you want to append the different files by columns:

all_dataframes = []
for idx, file_name in enumerate(files):
    df_temp = pd.read_csv(file_name, delimiter=',', header=None)
    column_prefix = 'f_' + str(idx) + '_'
    df_temp.columns = [column_prefix + c for c in column_names]
    df_temp[column_prefix + 'creation_time'] = time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime(file_name)))
    all_dataframes.append(df_temp)

pd.concat(all_dataframes, axis=1)

output:

enter image description here

Post a Comment for "Get File Created Date - Add To Dataframes Column On Read_csv"