Skip to content
Advertisement

How to create Dataframe with the columns names being as a part of a csv file path?

I have a root folder

JavaScript

With multiple folders in it with and ultimate paths to csv files:

JavaScript

I was managed to create a Dataframe containing all csv files concatenated using the following code:

JavaScript

Result:

JavaScript

But I am now struggling to add the respective date+name to the Dataframe, so it would look like this:

JavaScript

How can I do it?

Advertisement

Answer

With pathlib, you can go 1 & 2 directories up and get the name and date. Since this involves two things, an explicit for loop might be more readable than the list comprehension:

JavaScript

Or equivalently, the list comprehension counterpart is:

JavaScript

where we use assign to put new columns to each dataframe.

It depends on you to choose between explicit for loop or list comprehension.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement