簡體   English   中英

如何使用熊貓數據框作為參考制作路徑

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

我計划在數百個文件夾中使用 Excel 制作數百個數據框。

參考表:

        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

代碼示例:

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

圖案

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

筆記

我知道文件夾名稱正在使用空格,但我只想使用文件夾名稱中的單詞保存數據框,無論是正則表達式還是清理

首先在您的數據框中創建一個名為“路徑”的列。

df['Path'] = df['Folder'] + '/TopSites-' + df['Category'] + '_' + df['Sub Category'] + '-(999)-(2022_03).xlsx' # Create the whole 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