如何使用 pandas 数据框作为参考制作路径
How to make path using pandas dataframe as reference
我计划在数百个文件夹中使用 Excel 制作数百个数据框。
引用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
代码示例:
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')
备注
我知道文件夹名称使用 space,但我只想使用文件夹名称中的单词保存数据框,无论是正则表达式还是清理
首先在您的数据框中创建一个名为“Path”的列。
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)
我计划在数百个文件夹中使用 Excel 制作数百个数据框。
引用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
代码示例:
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')
备注
我知道文件夹名称使用 space,但我只想使用文件夹名称中的单词保存数据框,无论是正则表达式还是清理
首先在您的数据框中创建一个名为“Path”的列。
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)