'How to make path using pandas dataframe as reference

I plan to make hundreds of dataframe with using Excel in hundreds of folders.

Reference table:

        Folder                         Category           Sub Category
205     News and Media                 News and Media     News and Media
206     Vehicles - Motorcycles         Vehicles           Motorcycles
207     Vehicles - Motorsports         Vehicles           Motorsports
208     Vehicles - Other Vehicles      Vehicles           Other Vehicles

Code example:

Data_Vehicles-Motorcycles = pd.read_excel('[Folder]/TopSites-Vehicles_Motorcycles-(999)-(2022_03).xlsx','Aggregated_Data_for_Time_Period')

Pattern

Data_[Folder] = pd.read_excel('[Folder]/TopSites-[Category]_[Sub Category]-(999)-(2022_03).xlsx','Aggregated_Data_for_Time_Period')

Note

I know that the folder name is using space, but I just want to the the dataframe is saved using words in folder name, no matter the regex or cleansing



Solution 1:[1]

First create a column named "Path" in your dataframe.

df['Path'] = df['Folder'] + '/TopSites-' + df['Category'] + '_' + df['Sub Category'] + '-(999)-(2022_03).xlsx' # Create the whole path

After that, you can loop through your dataframe and read each path.

for path in df['Path']:
    folder = path.split('/')[0] # Get the folder from the path
    Data_[folder] = pd.read_excel(path)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nabih Bawazir