简体   繁体   English

如何使用熊猫数据框作为参考制作路径

[英]How to make path using pandas dataframe as reference

I plan to make hundreds of dataframe with using Excel in hundreds of folders.我计划在数百个文件夹中使用 Excel 制作数百个数据框。

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我知道文件夹名称正在使用空格,但我只想使用文件夹名称中的单词保存数据框,无论是正则表达式还是清理

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM