您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)python中如何實(shí)現(xiàn)excel讀取、統(tǒng)計(jì)、寫入,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
背景
圖像領(lǐng)域內(nèi)的一個(gè)國(guó)內(nèi)會(huì)議快要召開了,要發(fā)各種邀請(qǐng)郵件,之后要錄入、統(tǒng)計(jì)郵件回復(fù)(參會(huì)還是不參會(huì)等)。如此重要的任務(wù),老師就托付給我了。ps: 統(tǒng)計(jì)回復(fù)郵件的時(shí)候,能知道誰(shuí)參會(huì)或誰(shuí)不參會(huì)。
而我主要的任務(wù),除了錄入郵件回復(fù),就是統(tǒng)計(jì)理事和普通會(huì)員的參會(huì)情況了(參會(huì)的、不參會(huì)的、沒(méi)回復(fù)的)。錄入郵件回復(fù)信息沒(méi)辦法只能人工操作,但如果統(tǒng)計(jì)也要人工的話,那工作量就太大了(比如在上百人的列表中搜索另外上百人在不在此列表中?。。?,于是就想到了用python來(lái)幫忙,花兩天時(shí)間不斷修改,寫了6個(gè)版本。。。
摘要
version_1 基本實(shí)現(xiàn)了excel讀取、統(tǒng)計(jì)、顯示功能,但問(wèn)題也有不少,像顯示出來(lái)后還要自已復(fù)制、粘貼到excel表,而且set中還有nan這樣的bug。
version_2 相比較version_1而言,此版本用set代替list,可以自動(dòng)去重。
version_3 解決了set中出現(xiàn)nan的bug,而且還加入的excel寫入的功能,但一次只能寫入一張表,所以要運(yùn)行兩次才能寫入兩張表(sheet)。
version_4 的改進(jìn)在于將version_3中寫入兩張表格的操作,集成在一個(gè)程序里,只需要運(yùn)行一次便可寫入兩張表,但也總是會(huì)寫入兩張表,萬(wàn)一你只想寫入一張表呢??
version_5 相對(duì)之前版本的最大改進(jìn)在于將程序模塊化,更具可讀性了; 對(duì)修復(fù)set中出現(xiàn)nan的方法也進(jìn)行了改進(jìn)和簡(jiǎn)化; 而且可以自由控制寫入多少?gòu)埍砹恕?/p>
version_final 相比較version_5,修復(fù)了一個(gè)bug,之前需要先驗(yàn)知識(shí),現(xiàn)在更通用一點(diǎn)(prep函數(shù)取代了set2list函數(shù))。
version_1
基本實(shí)現(xiàn)了excel讀取、統(tǒng)計(jì)、顯示功能,但問(wèn)題也有不少,像顯示出來(lái)后還要自已復(fù)制、粘貼到excel表,而且set中還有nan這樣的值。
#version_1 import os import numpy as np import pandas as pd os.chdir('C:\\Users\\dell\\Desktop\\0711任務(wù)') print(os.getcwd()) data = pd.read_excel('for_python.xlsx','Sheet2') return_set = set(data['回執(zhí)名單']) demand_set = set(data['理事名單']) answer_list = [] unanswer_list = [] for each in demand_set: if each in return_set: answer_list.append(each) else: unanswer_list.append(each) notattend_set = set(data['回執(zhí)名單'][-15:]) nt = [] for each in notattend_set: if each in answer_list: nt.append(each) def disp(ll, cap, num = True): print(cap) if num: for i, each in enumerate(ll): print(i+1,each) else: for each in enumerate(ll): print(each) disp(answer_list,'\n理事回執(zhí)名單') disp(unanswer_list,'\n理事未回執(zhí)名單') disp(nt,'\n理事回執(zhí)說(shuō)不參加名單')
version_2
相比較上一個(gè)版本,此版本用set代替list,可以自動(dòng)去重。
#version_2 import os import numpy as np import pandas as pd os.chdir('C:\\Users\\dell\\Desktop\\0711任務(wù)') print(os.getcwd()) data = pd.read_excel('for_python.xlsx','Sheet2') return_set = set(data['回執(zhí)名單']) demand_set = set(data['理事名單']) answer_set = set([]) #理事回執(zhí)名單 unanswer_set = set([]) #理事未回執(zhí)名單 for each in demand_set: if each in return_set: answer_set.add(each) else: unanswer_set.add(each) notattend_set = set(data['回執(zhí)名單'][-17:]) nt = set([]) #理事回執(zhí)說(shuō)不參加名單 for each in notattend_set: if each in answer_set: nt.add(each) ans_att_set = answer_set - nt #理事回執(zhí)參加名單 def disp(ss, cap, num = False): print(cap) if num: for i, each in enumerate(ss): print(i+1,each) else: for each in ss: print(each) #disp(answer_set,'\n理事回執(zhí)名單') disp(ans_att_set,'\n理事回執(zhí)說(shuō)參加名單') disp(nt,'\n理事回執(zhí)說(shuō)不參加名單') disp(unanswer_set,'\n理事未回執(zhí)名單') print(len(ans_att_set),len(nt),len(unanswer_set))
version_3
此版本解決了set中出現(xiàn)nan的bug,而且還加入的excel寫入的功能,但一次只能寫入一張表,所以要運(yùn)行兩次才能寫入兩張表(sheet)。
step_1
import os import numpy as np import pandas as pd os.chdir('C:\\Users\\dell\\Desktop') print('work_directory: ', os.getcwd()) data = pd.read_excel('理事與會(huì)員名單.xlsx','理事與會(huì)員名單') #1.載入excel,得到三個(gè)名單 ans_attend_set = set(data['回執(zhí)參加']) #回執(zhí)參會(huì)名單 N = len(ans_attend_set) ans_notatt_idx = [i for i in range(N) if type(data['回執(zhí)不參加'][i]) == np.float][0] ans_notatt_set = set(data['回執(zhí)不參加'][:ans_notatt_idx])#回執(zhí)不參會(huì)名單 concil_idx = [i for i in range(N) if type(data['理事名單'][i]) == np.float][0] concil_set = set(data['理事名單'][:concil_idx]) #理事名單 #2.統(tǒng)計(jì)理事參會(huì)情況 concil_attend_set = set([]) #理事回執(zhí)參會(huì)名單 concil_notatt_set = set([]) #理事回執(zhí)不參會(huì)名單 concil_notans_set = set([]) #理事未回執(zhí)名單 for each in concil_set: if each in ans_attend_set: concil_attend_set.add(each) elif each in ans_notatt_set: concil_notatt_set.add(each) else: concil_notans_set.add(each) #3. 顯示結(jié)果 def disp(ss, cap, num = True): #ss: 名單集合 #cap: 開頭描述 print(cap,'({})'.format(len(ss))) for i in range(np.ceil(len(ss)/5).astype(int)): pre = i * 5 nex = (i+1) * 5 #調(diào)整顯示格式 dd = '' for each in list(ss)[pre:nex]: if len(each) == 2: dd = dd + ' ' + each elif len(each) == 3: dd = dd + ' ' + each else: dd = dd + '' + each print('{:3.0f} -{:3.0f} {}'.format(i*5+1,(i+1)*5,dd)) disp(concil_attend_set,'\n參會(huì)理事') disp(concil_notatt_set,'\n不參會(huì)理事') disp(concil_notans_set,'\n未回執(zhí)理事') #4. 將理事參會(huì)情況,寫入excel df = pd.DataFrame(list(concil_attend_set),columns = ['參會(huì)理事']) df['']=pd.DataFrame(['']) df['序號(hào)1'] = pd.DataFrame(np.arange(len(concil_notatt_set))+1) df['不參會(huì)理事'] = pd.DataFrame(list(concil_notatt_set)) df['_']=pd.DataFrame(['']) df['序號(hào)2'] = pd.DataFrame(np.arange(len(concil_notans_set))+1) df['未回執(zhí)理事'] = pd.DataFrame(list(concil_notans_set)) df.index = df.index + 1 df.to_excel('理事和會(huì)員回執(zhí)統(tǒng)計(jì).xlsx', sheet_name='理事回執(zhí)統(tǒng)計(jì)') print('\n\n寫入excel成功~~')
step_2
import os import numpy as np import pandas as pd os.chdir('C:\\Users\\dell\\Desktop') print('work_directory: ', os.getcwd()) data = pd.read_excel('理事與會(huì)員名單.xlsx','理事與會(huì)員名單') #1.載入excel,得到三個(gè)名單 ans_attend_set = set(data['回執(zhí)參加']) #回執(zhí)參會(huì)名單 N = len(ans_attend_set) ans_notatt_idx = [i for i in range(N) if type(data['回執(zhí)不參加'][i]) == np.float][0] ans_notatt_set = set(data['回執(zhí)不參加'][:ans_notatt_idx])#回執(zhí)不參會(huì)名單 mem_idx = [i for i in range(N) if type(data['被推薦人'][i]) == np.float][0] mem_set = set(data['被推薦人'][:mem_idx]) #被推薦為會(huì)員代表名單 #2.統(tǒng)計(jì)會(huì)員參會(huì)情況 mem_attend_set = set([]) #回執(zhí)參會(huì)會(huì)員 mem_notatt_set = set([]) #回執(zhí)不參會(huì)會(huì)員 mem_notans_set = set([]) #未回執(zhí)會(huì)員 for each in mem_set: if each in ans_attend_set: mem_attend_set.add(each) elif each in ans_notatt_set: mem_notatt_set.add(each) else: mem_notans_set.add(each) #3. 顯示結(jié)果 def disp(ss, cap, num = True): #ss: 名單集合 #cap: 開頭描述 print(cap,'({})'.format(len(ss))) for i in range(np.ceil(len(ss)/5).astype(int)): pre = i * 5 nex = (i+1) * 5 #調(diào)整顯示格式 dd = '' for each in list(ss)[pre:nex]: if len(each) == 2: dd = dd + ' ' + each elif len(each) == 3: dd = dd + ' ' + each else: dd = dd + '' + each print('{:3.0f} -{:3.0f} {}'.format(i*5+1,(i+1)*5,dd)) disp(mem_attend_set,'\n參會(huì)會(huì)員') disp(mem_notatt_set,'\n不參會(huì)會(huì)員') disp(mem_notans_set,'\n未回執(zhí)會(huì)員') #4. 將會(huì)員參會(huì)情況,寫入excel if len(mem_attend_set) > len(mem_notans_set): print('#1') L = len(mem_attend_set) mem_notans_list = list(mem_notans_set) mem_notans_list.extend([''] * (L - len(mem_notans_set))) mem_attend_list = list(mem_attend_set) else: print('#2') L = len(mem_notans_set) mem_attend_list = list(mem_attend_set) mem_attend_list.extend([''] * (L - len(mem_attend_set))) mem_notans_list = list(mem_notans_set) df = pd.DataFrame(mem_attend_list,columns = ['參會(huì)會(huì)員']) df['']=pd.DataFrame(['']) if len(mem_notatt_set) == 0: df['序號(hào)1'] = np.NaN df['不參會(huì)會(huì)員'] = np.NaN else: df['序號(hào)1'] = pd.DataFrame(np.arange(len(mem_notatt_set))+1) df['不參會(huì)會(huì)員'] = pd.DataFrame(list(mem_notatt_set)) df['_']=pd.DataFrame(['']) df['序號(hào)2'] = pd.DataFrame(np.arange(len(mem_notans_set))+1) df['未回執(zhí)會(huì)員'] = pd.DataFrame(mem_notans_list) df.index = df.index + 1 df0 = pd.read_excel('理事和會(huì)員回執(zhí)統(tǒng)計(jì).xlsx',sheet_name='理事回執(zhí)統(tǒng)計(jì)') writer = pd.ExcelWriter('理事和會(huì)員回執(zhí)統(tǒng)計(jì).xlsx') df0.to_excel(writer, sheet_name='理事回執(zhí)統(tǒng)計(jì)') df.to_excel(writer, sheet_name='會(huì)員回執(zhí)統(tǒng)計(jì)') writer.save() print('\n\n寫入excel成功~~')
version_4
version_4的改進(jìn)在于將version_3中寫入兩張表格的操作,集成在一個(gè)程序里,只需要運(yùn)行一次便可寫入兩張表,也總是會(huì)寫入兩張表。問(wèn)題是要是你只想寫入一張表呢??
import os import numpy as np import pandas as pd os.chdir('C:\\Users\\dell\\Desktop') print('work_directory: ', os.getcwd()) loadfile_sheet = ['理事與會(huì)員名單.xlsx','理事與會(huì)員名單'] columns = ['回執(zhí)參加','回執(zhí)不參加','理事','會(huì)員'] savefile_sheet = ['理事和會(huì)員回執(zhí)統(tǒng)計(jì).xlsx','理事回執(zhí)統(tǒng)計(jì)','會(huì)員回執(zhí)統(tǒng)計(jì)'] display = [1,1] def main(loadfile_sheet,columns,savefile_sheet,display): #1. 載入excel,得到名單 data = pd.read_excel(loadfile_sheet[0],loadfile_sheet[1]) def first_nan_index(pd): for i, each in enumerate(pd): if type(each) == np.float: return i return i idx = first_nan_index(data[columns[0]]) ans_attend_set = set(data[columns[0]][:idx])#回執(zhí)參會(huì)名單 idx = first_nan_index(data[columns[1]]) ans_notatt_set = set(data[columns[1]][:idx])#回執(zhí)不參會(huì)名單 idx = first_nan_index(data[columns[2]]) concil_set = set(data[columns[2]][:idx])#理事名單 idx = first_nan_index(data[columns[3]]) mem_set = set(data[columns[3]][:idx])#會(huì)員名單 #2. 統(tǒng)計(jì)參會(huì)情況 concil_attend_set = set([]) #回執(zhí)參會(huì)理事 concil_notatt_set = set([]) #回執(zhí)不參會(huì)理事 concil_notans_set = set([]) #未回執(zhí)理事 for each in concil_set: if each in ans_attend_set: concil_attend_set.add(each) elif each in ans_notatt_set: concil_notatt_set.add(each) else: concil_notans_set.add(each) mem_attend_set = set([]) #回執(zhí)參會(huì)會(huì)員 mem_notatt_set = set([]) #回執(zhí)不參會(huì)會(huì)員 mem_notans_set = set([]) #未回執(zhí)會(huì)員 for each in mem_set: if each in ans_attend_set: mem_attend_set.add(each) elif each in ans_notatt_set: mem_notatt_set.add(each) else: mem_notans_set.add(each) #3. 是否顯示中間結(jié)果 def disp(ss, cap, num = True): #ss: 名單集合 #cap: 開頭描述 print(cap,'({})'.format(len(ss))) for i in range(np.ceil(len(ss)/5).astype(int)): pre = i * 5 nex = (i+1) * 5 #調(diào)整顯示格式 dd = '' for each in list(ss)[pre:nex]: if len(each) == 2: dd = dd + ' ' + each elif len(each) == 3: dd = dd + ' ' + each else: dd = dd + '' + each print('{:3.0f} -{:3.0f} {}'.format(i*5+1,(i+1)*5,dd)) if display[0]: disp(concil_attend_set,'\n參會(huì)理事') disp(concil_notatt_set,'\n不參會(huì)理事') disp(concil_notans_set,'\n未回執(zhí)理事') if display[1]: disp(mem_attend_set,'\n參會(huì)會(huì)員') disp(mem_notatt_set,'\n不參會(huì)會(huì)員') disp(mem_notans_set,'\n未回執(zhí)會(huì)員') #4. 寫入excel def trans_pd(df,ss,cap,i=1): if len(ss) == 0: df['序號(hào){}'.format(i)] = np.NaN df[cap] = np.NaN else: df['序號(hào){}'.format(i)] = pd.DataFrame(np.arange(len(ss))+1) df[cap] = pd.DataFrame(list(ss)) df['_'*i]=pd.DataFrame(['']) return df def set2list(mem_attend_set,mem_notans_set): if len(mem_attend_set) > len(mem_notans_set): L = len(mem_attend_set) mem_notans_list = list(mem_notans_set) mem_notans_list.extend([''] * (L - len(mem_notans_set))) mem_attend_list = list(mem_attend_set) else: L = len(mem_notans_set) mem_attend_list = list(mem_attend_set) mem_attend_list.extend([''] * (L - len(mem_attend_set))) mem_notans_list = list(mem_notans_set) return mem_attend_list,mem_notans_list mem_attend_list, mem_notans_list = set2list(mem_attend_set, mem_notans_set) df1 = pd.DataFrame(mem_attend_list,columns = ['參會(huì)會(huì)員']) df1['']=pd.DataFrame(['']) df1 = trans_pd(df1,mem_notatt_set,'不參會(huì)會(huì)員') df1 = trans_pd(df1,mem_notans_set,'未回執(zhí)會(huì)員',2) df1.index = df1.index + 1 concil_attend_list, concil_notans_list = set2list(concil_attend_set, concil_notans_set) df2 = pd.DataFrame(concil_attend_list,columns = ['參會(huì)理事']) df2['']=pd.DataFrame(['']) df2 = trans_pd(df2,concil_notatt_set,'不參會(huì)理事') df2 = trans_pd(df2,concil_notans_list,'未回執(zhí)理事',2) df2.index = df2.index + 1 writer = pd.ExcelWriter(savefile_sheet[0]) df2.to_excel(writer, sheet_name=savefile_sheet[1]) df1.to_excel(writer, sheet_name=savefile_sheet[2]) writer.save() print('\n\n寫入excel成功~~') if __name__ == '__main__': main(loadfile_sheet,columns,savefile_sheet,display)
version_5
version_5對(duì)修復(fù)set中出現(xiàn)nan的方法進(jìn)行了改進(jìn)和簡(jiǎn)化; 而且將程序模塊化,更具可讀性; 可以自由控制寫入多少?gòu)埍砹恕?/p>
import os import numpy as np import pandas as pd os.chdir('C:\\Users\\dell\\Desktop') print('work_directory: ', os.getcwd()) loadfile_sheet = ['理事與會(huì)員名單.xlsx','理事與會(huì)員名單'] common_columns = ['回執(zhí)參加','回執(zhí)不參加'] concerned_columns = ['理事','會(huì)員'] disp_columns = ['參會(huì)','不參會(huì)','未回執(zhí)'] savefile_sheet = ['理事和會(huì)員回執(zhí)統(tǒng)計(jì).xlsx','理事回執(zhí)統(tǒng)計(jì)','會(huì)員回執(zhí)統(tǒng)計(jì)'] def disp(ss, cap, num = True): #ss: 名單集合 #cap: 開頭描述 print(cap,'({})'.format(len(ss))) for i in range(np.ceil(len(ss)/5).astype(int)): pre = i * 5 nex = (i+1) * 5 #調(diào)整顯示格式 dd = '' for each in list(ss)[pre:nex]: if len(each) == 2: dd = dd + ' ' + each elif len(each) == 3: dd = dd + ' ' + each else: dd = dd + '' + each print('{:3.0f} -{:3.0f} {}'.format(i*5+1,(i+1)*5,dd)) def trans_pd(df,ss,cap,i=1): df['_'*i]=pd.DataFrame(['']) if len(ss) == 0: df['序號(hào){}'.format(i)] = np.NaN df[cap] = np.NaN else: df['序號(hào){}'.format(i)] = pd.DataFrame(np.arange(len(ss))+1) df[cap] = pd.DataFrame(list(ss)) return df def set2list(ss1,ss2): if len(ss1) > len(ss2): L = len(ss1) ss2_list = list(ss2) ss2_list.extend([''] * (L - len(ss2))) ss1_list = list(ss1) else: L = len(ss2) ss1_list = list(ss1) ss1_list.extend([''] * (L - len(ss1))) ss2_list = list(ss2) return ss1_list,ss2_list def get_df(loadfile_sheet,common_columns,concerned_column,disp_columns, display = True): #1. 載入excel data = pd.read_excel(loadfile_sheet[0],loadfile_sheet[1]) common_set1 = set(data[common_columns[0]]) common_set1.discard(np.NaN) common_set2 = set(data[common_columns[1]]) common_set2.discard(np.NaN) concerned_set = set(data[concerned_column]) concerned_set.discard(np.NaN) #2. 統(tǒng)計(jì) concerned_in_set_1 = set([]) concerned_in_set_2 = set([]) concerned_in_no_set = set([]) for each in concerned_set: if each in common_set1: concerned_in_set_1.add(each) elif each in common_set2: concerned_in_set_2.add(each) else: concerned_in_no_set.add(each) #3. 顯示 if display: disp(concerned_in_set_1,'\n'+disp_columns[0]+concerned_column) disp(concerned_in_set_2,'\n'+disp_columns[1]+concerned_column) disp(concerned_in_no_set,'\n'+disp_columns[2]+concerned_column) #4. 返回DataFrame concerned_in_set_1_list, concerned_in_set_2_list = set2list(concerned_in_set_1, concerned_in_no_set) df = pd.DataFrame(concerned_in_set_1_list,columns = [disp_columns[0]]) df = trans_pd(df,concerned_in_set_2,disp_columns[1]) df = trans_pd(df,concerned_in_no_set,disp_columns[2],2) df.index = df.index + 1 return df def save2excel(df, concerned_column, savefile_sheet): L = len(savefile_sheet) - 1 idx = 0 for i in np.arange(L)+1: if concerned_column in savefile_sheet[i]: idx = i break if idx != 0: names = locals() for i in np.arange(L)+1: if i != idx: names['df%s' % i] = pd.read_excel(savefile_sheet[0], sheet_name=savefile_sheet[i]) writer = pd.ExcelWriter(savefile_sheet[0]) for i in np.arange(L)+1: if i != idx: names['df%s' % i].to_excel(writer, sheet_name=savefile_sheet[i]) else: df.to_excel(writer, sheet_name=savefile_sheet[i]) writer.save() else: names = locals() for i in np.arange(L)+1: names['df%s' % i] = pd.read_excel(savefile_sheet[0], sheet_name=savefile_sheet[i]) writer = pd.ExcelWriter(savefile_sheet[0]) for i in np.arange(L)+1: names['df%s' % i].to_excel(writer, sheet_name=savefile_sheet[i]) df.to_excel(writer, sheet_name=concerned_column) writer.save() print('writing success') if __name__ == '__main__': for concerned_column in concerned_columns: df = get_df(loadfile_sheet,common_columns, concerned_column,disp_columns, display = True) save2excel(df, concerned_column, savefile_sheet)
version_final
相比較version_5,修復(fù)了一個(gè)bug,之前需要先驗(yàn)知識(shí),現(xiàn)在更通用一點(diǎn)(prep函數(shù)取代了set2list函數(shù))。
import os import numpy as np import pandas as pd os.chdir('C:\\Users\\dell\\Desktop') print('work_directory: ', os.getcwd()) loadfile_sheet = ['理事與會(huì)員名單.xlsx','理事與會(huì)員名單'] common_columns = ['回執(zhí)參加','回執(zhí)不參加'] concerned_columns = ['理事','會(huì)員'] disp_columns = ['參會(huì)','不參會(huì)','未回執(zhí)'] savefile_sheet = ['理事和會(huì)員回執(zhí)統(tǒng)計(jì).xlsx','理事回執(zhí)統(tǒng)計(jì)','會(huì)員回執(zhí)統(tǒng)計(jì)'] def disp(ss, cap, num = True): #功能:顯示名單 #ss : 名單集合 #cap :開頭描述 print(cap,'({})'.format(len(ss))) for i in range(np.ceil(len(ss)/5).astype(int)): pre = i * 5 nex = (i+1) * 5 #調(diào)整顯示格式 dd = '' for each in list(ss)[pre:nex]: if len(each) == 2: dd = dd + ' ' + each elif len(each) == 3: dd = dd + ' ' + each else: dd = dd + '' + each print('{:3.0f} -{:3.0f} {}'.format(i*5+1,(i+1)*5,dd)) def trans_pd(df,ll,cap,i=1): #功能:生成三列--空列、序號(hào)列、數(shù)據(jù)列 #df : DataFrame結(jié)構(gòu) #ll : 列表 #cap : 顯示的列名 #i : 控制空列的名字 df['_'*i]=pd.DataFrame(['']) if len(set(ll)) == 1: df['序號(hào){}'.format(i)] = np.NaN df[cap] = np.NaN else: df['序號(hào){}'.format(i)] = pd.DataFrame(np.arange(len(set(ll))-1)+1) df[cap] = pd.DataFrame(ll) return df def prep(ss, N): #功能:預(yù)處理,生成列表,并補(bǔ)齊到長(zhǎng)度N #ss : 集體 #N :長(zhǎng)度 ll = list(ss) L = len(ll) ll.extend([np.NaN] * (N-L)) return ll def get_df(loadfile_sheet,common_columns,concerned_column,disp_columns, display = True): #1. 載入excel data = pd.read_excel(loadfile_sheet[0],loadfile_sheet[1]) common_set1 = set(data[common_columns[0]]) common_set2 = set(data[common_columns[1]]) concerned_set = set(data[concerned_column]) common_set1.discard(np.NaN) common_set2.discard(np.NaN) concerned_set.discard(np.NaN) #2. 統(tǒng)計(jì) concerned_in_set_1 = set([]) concerned_in_set_2 = set([]) concerned_in_no_set = set([]) for each in concerned_set: if each in common_set1: concerned_in_set_1.add(each) elif each in common_set2: concerned_in_set_2.add(each) else: concerned_in_no_set.add(each) #3. 顯示 if display: disp(concerned_in_set_1,'\n'+disp_columns[0]+concerned_column) disp(concerned_in_set_2,'\n'+disp_columns[1]+concerned_column) disp(concerned_in_no_set,'\n'+disp_columns[2]+concerned_column) #4. 返回DataFrame N = np.max([len(concerned_in_set_1),len(concerned_in_set_2),len(concerned_in_no_set)]) concerned_in_set_1_list = prep(concerned_in_set_1,N) concerned_in_set_2_list = prep(concerned_in_set_2,N) concerned_in_no_list = prep(concerned_in_no_set,N) df = pd.DataFrame(concerned_in_set_1_list,columns = [disp_columns[0]]) df = trans_pd(df,concerned_in_set_2_list,disp_columns[1]) df = trans_pd(df,concerned_in_no_list,disp_columns[2],2) df.index = df.index + 1 return df def save2excel(df, concerned_column, savefile_sheet): L = len(savefile_sheet) - 1 idx = 0 for i in np.arange(L)+1: if concerned_column in savefile_sheet[i]: idx = i break if idx != 0: #如果有對(duì)應(yīng)sheet names = locals() for i in np.arange(L)+1: if i != idx: names['df%s' % i] = pd.read_excel(savefile_sheet[0], sheet_name=savefile_sheet[i]) writer = pd.ExcelWriter(savefile_sheet[0]) for i in np.arange(L)+1: if i != idx: names['df%s' % i].to_excel(writer, sheet_name=savefile_sheet[i]) else: df.to_excel(writer, sheet_name=savefile_sheet[i]) writer.save() else: #如果沒(méi)有對(duì)應(yīng)sheet,創(chuàng)建一個(gè)新sheet names = locals() for i in np.arange(L)+1: names['df%s' % i] = pd.read_excel(savefile_sheet[0], sheet_name=savefile_sheet[i]) writer = pd.ExcelWriter(savefile_sheet[0]) for i in np.arange(L)+1: names['df%s' % i].to_excel(writer, sheet_name=savefile_sheet[i]) df.to_excel(writer, sheet_name=concerned_column) writer.save() print('writing success') if __name__ == '__main__': for concerned_column in concerned_columns: df = get_df(loadfile_sheet,common_columns, concerned_column,disp_columns, display = True) save2excel(df, concerned_column, savefile_sheet)
關(guān)于“python中如何實(shí)現(xiàn)excel讀取、統(tǒng)計(jì)、寫入”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。