Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Column with end of file name python

I have a code that merges all txt files from a directory into a dataframe

follow the code below

import pandas as pd
import os
import glob

diretorio = "F:\PROJETOS\LOTE45\ARQUIVOS\RISK\RISK_CUSTOM_FUND_N1" 
files = [] 

files = [pd.read_csv(file, delimiter='\t')
     for file in glob.glob(os.path.join(diretorio ,"*.txt"))]


df = pd.concat(files, ignore_index=True)
df

that gives result to this table

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

I needed to add a date column to this table, but I only have the date available at the end of the filename.

enter image description here

How can I get the date at the end of the filename and put it inside the dataframe.

I have no idea how to do this

>Solution :

Assuming the file structure is constant, you can parse the end of the filename for every iteration of the loop this way :-

from datetime import datetime

files = []

for file in glob.glob(os.path.join(diretorio ,"*.txt")):
  df_f = pd.read_csv(file, delimiter='\t')
  df_f['date'] = datetime.strptime(file[-11:-4], "%d%m%Y")
  files.append(df_f)


df = pd.concat(files, ignore_index=True)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading