Extract Filename to DataFrame Column

Hi friends,

i have a file with a date as filename e. g. 01012021.xlsx.
I create a new DataFrame based on this file in Pandas.
Now i want to have a column with the Date which is in the File as a TimeStamp.

How can i extract the filename and write it to a column into my DF?

Thanks in advance for any help, very appreciated.

Hi Simo, first step
Parse date and time from columns, see here and use argument parse_dates
https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
Second step, after creating dataframe
Rename required columns - see here
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rename.html

Hi Vadim,

thanks for your answer.
I’m not sure if i explained it well but i want to parse the date from the filename not from columns in the file.

Maybe my explanation was not exact enough :slight_smile:
So if filename i’m importing with df.to_excel(r"(C:\Users\Desktop\01052021.xlsx") i need a new column in my Dataframe with the Date 01052021.

Then use pandas.DataFrame.insert method.
You must designate column number in dataframe and their name and extract from csv only required columns and transfer it as value = pd.read_csv(“file_name”, “column_name”, “parseasdate”) to insert method

1 Like

Спасибо, Вадик! )

Thank you Vadik :slight_smile:

1 Like

Don’t mention, young komsomol member - know and love the official documentation for Pandas API and Stack OverFlow)))
P.S I am young komsomol member too))