python同一个文件中不可以编写多个函数_如何在Python中编写在20个不同的csv文件上运行该函数的函数?...

defdoAll(baseBatchPath,numberOfBatches):batchResultListAll=[]# this will store the resulted dataframesspath='d:\\a2\\studentlist.csv'q1path='d:\\a2\\quiz\\quiz1.csv'q2path='d:\\a2\\quiz\\quiz2.csv'tpath='d:\\a2\\testcasestudent.txt'# the final dataframe that needs to be created and filled up eventuallyidx=pd.MultiIndex.from_product([['batch1','batch2','batch3','batch4','batch9'],['quiz1','quiz2']])cols=['noofpresent','lesserthan50','between50and60','between60and70','between70and80','greaterthan80']statdf=pd.DataFrame('-',idx,cols)# Master list of all who attended Quiz1q1=pd.read_csv(q1path,usecols=['Firstname','Grade/10.00','State'],na_values=['-','In progress',np.NaN])q1.dropna(inplace=True)q1['Grade/10.00']=q1['Grade/10.00']*10# Master list of all who attended Quiz2q2=pd.read_csv(q2path,usecols=['Firstname','Grade/10.00','State'],na_values=['-','In progress',np.NaN])q2.dropna(inplace=True)q2['Grade/10.00']=q2['Grade/10.00']*10# generate each batch file path and do other worksforbatchIdinrange(numberOfBatches-1):batchCsvPath=baseBatchPath+str(batchId+1)+".csv"# Master list of students in Batch 9batch=pd.read_csv(batchCsvPath,usecols=['studentName','admissionNumber'])batch.rename(columns={'studentName':'Firstname'},inplace=True)# Merge eachBatch list of names to list of quiz1 on their firstname columnq1batch=pd.merge(batch,q1)q1batch=q1.loc[q1['Firstname'].isin(batch.Firstname)]# checking if the name exits in either listsq1batch.reset_index(inplace=True)#print(q1batch)lt50=q1batch.loc[(q1batch['Grade/10.00']<50)]#findout list of students whose grades are lesser than 50outBatchq1=(lt50['Grade/10.00'].count())# print(outBatchq1) to just get the count of number of students who got <50 quiz1 from batch -> batchId#do same for quiz 2# Merge each Batch to Q2q2batch=pd.merge(batch,q2)q2batch=q2.loc[q2['Firstname'].isin(batch.Firstname)]q2batch.reset_index(inplace=True)q2batch.loc[(q2batch['Grade/10.00']<=50)].count()lt50=q2batch.loc[(q2batch['Grade/10.00']<50)]outBatchq2=(lt50['Grade/10.00'].count())# print(outBatchq2)# finally save the resulted DF for later usebatchResultListAll.append([q1batch,q2batch])#call the function using base path and number of batch csv filesdoAll("d:\\\\a2\\\\batchwiselist\\\\",18)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值