【发布时间】:2020-04-01 19:25:51
【问题描述】:
我有一个脚本,该脚本从 excel 文件中提取样本并将该样本作为 csv 输出。如何遍历包含多个 excel 文件的文件夹以避免每次运行脚本时更改文件的任务?我相信我可以使用 glob,但这似乎只是将所有 excel 文件合并在一起。
import pandas as pd
import glob
root_dir = r"C:\Users\bryanmccormack\Desktop\Test_Folder\*.xlsx"
excel_files = glob.glob(root_dir, recursive=True)
for xls in excel_files:
df_excel = pd.read_excel(xls)
df_excel = df_excel.loc[(df_excel['Track Item']=='Y')]
def sample_per(df_excel):
if len(df_excel) <= 10000:
return df_excel.sample(frac=0.05)
elif len(df_excel) >= 15000:
return df_excel.sample(frac=0.03)
else:
return df_excel.sample(frac=0.01)
final = sample_per(xls)
df_excel.loc[df_excel['Retailer Item ID'].isin(final['Retailer Item ID']), 'Track Item'] = 'Audit'
df_excel.to_csv('Testicle.csv',index=False)
【问题讨论】:
-
但它不起作用会发生什么?