python pandas讀取excel文件代碼 pandas 寫入excel不顯示格式?
pandas 寫入excel不顯示格式?使用pandas格式化excpython與vba處理數(shù)據(jù)的區(qū)別?有一個csv文件,包含CNUM和COMPANY兩列,數(shù)據(jù)包含空行和重復行。要求:1)刪除空白行;
pandas 寫入excel不顯示格式?
使用pandas格式化exc
python與vba處理數(shù)據(jù)的區(qū)別?
有一個csv文件,包含CNUM和COMPANY兩列,數(shù)據(jù)包含空行和重復行。
要求:
1)刪除空白行;
2)重復的行數(shù)據(jù)只保留一行有效數(shù)據(jù);
3)將公司列的名稱改為company _ new ;
4)增加六列,即C _ COL, d _ COL ;e _ COL ;f _ COL ;G _ COL和 H _ COL。
首先,使用python熊貓來處理:將熊貓作為pd導入
將numpy作為np導入
從熊貓進口數(shù)據(jù)框,系列
d:
文件_對象打開(文件路徑)
Df_csv(file_obj) #讀取csv文件并創(chuàng)建數(shù)據(jù)幀。
Df (columns [cnum,company,c _ col,d _ col,e _ col,f _ col,g _ col,h _ col],fill _ valu:公司_新},到位真)#修改欄目名稱。
Ddff.dropna (axis0,howall) #刪除NAN,即文件中的空行。
Df [CNUM] df [cnum]。as type (int32) #將cnum列的數(shù)據(jù)類型指定為int32。
ddf . drop _ duplicates(subset[cnum,company _ new],keep first) #刪除重復行。
_csv(newpath,indexFalse,encodingGBK)
文件_()
如果__nam:
fil: users 12078 desktoppythocnum _ company . CSV
fil: users 12078 desktoppythocnum _ COMPANY _ output . CSV
處理數(shù)據(jù)(文件路徑,文件)保存路徑)
二、用VBA應對:選項基數(shù)1
選項顯式
副總管()
出錯時轉(zhuǎn)到錯誤處理
將wb標注為工作簿
將wb_out標注為工作簿
Dim sht作為工作表
Dim sht_out作為工作表
變暗rng As范圍
將已使用的行作為字節(jié)顯示
Dim usedrows_out As Byte
Dim dict_cnum_company作為賓語
Dim字符串文件路徑作為字符串
Dim str_n:賦值
str _ file _ path c: users 12078 desktoppythocnum _ company . CSV
str _ new _ file _ path c: users 12078 desktoptythocnum _ COMPANY _ output . CSV
設(shè)置WB checkAndAttachWorkbook(str _ file _ path)
設(shè)置sht (CNUM_COMPANY)
設(shè)置wb_out
創(chuàng)建一個CSV文件
設(shè)置sht_out wb_(CNUM_COMPANY_OUTPUT)
設(shè)置dict_cnum_company CreateObject(腳本。字典)
usedrows(getlastvidrow(sht,A),getlastvidrow(sht,B))
將標題COMPANY重命名為Company_New,刪除空白的重復行/行。
Dim cnum_company作為字符串
cnum _公司
對于sht中的每個rng。范圍(A1,A已用行數(shù))
If ((0,1)。價值)公司
(0, 1).價值公司_新建
如果…就會結(jié)束
cnum_company - (0,1)。價值
If (cnum_company) -而不是dict_cnum_company。存在(- (0,1)。值)那么
dict_cnum_ - (0,1)。值,
如果…就會結(jié)束
下一個rng
循環(huán)dict的關(guān)鍵字,將關(guān)鍵字拆分成cnum數(shù)組和company數(shù)組。
Dim index_dict As Byte
Dim arr_cnum()
Dim arr _公司()
對于index_dict 0到UBound(dict_cnum_)
ReDim Preserve arr_cnum(1到UBound(dict_cnum_) 1)
ReDim Preserve arr_Company(1到UBound(dict_cnum_) 1)
arr _ cnum(index _ dict 1)Split(dict _ cnum _()(index _ dict),-)(0)
arr _ Company(index _ dict 1)Split(dict _ cnum _()(index _ dict),-)(1)
索引_字典
然后
將數(shù)組的值賦給單元格。
噓_出。Range(A1,A UBound(arr_cnum)) (arr_cnum)
噓_出。范圍(B1,B下限(arr_Company)) (arr_Company)
添加6列以輸出CSV fil:
Dim arr_columns()作為變量
arr_columns數(shù)組(C_col,D_col,E_col,F(xiàn)_col,G_col,H_col)
噓_出。范圍(C1:H1) arr_columns
調(diào)用checkAndCloseWorkbook(str _ file _ path,F(xiàn)alse)
調(diào)用checkAndClos:
調(diào)用checkAndCloseWorkbook(str _ file _ path,F(xiàn)alse)
調(diào)用checkAndCloseWorkbook(str _ new _ file _ path,F(xiàn)alse)
末端接頭
輔助功能:
獲取工作表中第N列的最后一行
函數(shù)getLastValidRow(in_ws作為工作表,in_col作為字符串)
getLastValidRow in_ws。單元格(in_,in_col)。結(jié)束(xlUp)。排
結(jié)束功能
函數(shù)checkAndAttachWorkbook(in _ WB _ path As String)作為工作簿
將wb標注為工作簿
將mywb顯示為字符串
mywb in_wb_path
對于工作簿中的每個wb
如果LCase(wb。全名)LCase(mywb) Then
設(shè)置checkAndAttachWorkbook wb
退出功能
如果…就會結(jié)束
然后
設(shè)置wb (in_wb_path,Updat:0)
設(shè)置checkAndAttachWorkbook wb
結(jié)束功能
函數(shù)checkAndCloseWorkbook(in _ WB _ path為字符串,in_saved為布爾值)
將wb標注為工作簿
將mywb顯示為字符串
mywb in_wb_path
對于工作簿中的每個wb
如果LCase(wb。全名)LCase(mywb) Then
savechang:in _ saved
退出功能
如果…就會結(jié)束
然后
結(jié)束功能
三、輸出結(jié)果:
兩種方法的輸出結(jié)果是相同的:
四、對比總結(jié):
Python熊貓有很多內(nèi)置的地方。管理數(shù)據(jù)的方法,我們不 不需要反復制作輪子,使用起來非常方便,代碼也簡單很多。
Excel VBA通過使用數(shù)組、字典等數(shù)據(jù)結(jié)構(gòu)(實際需求中,數(shù)據(jù)量往往很大,所以有些地方?jīng)]有直接遍歷單元格的方法),以及處理字符串、數(shù)組、字典的多種方法來處理這個需求。文件的操作也很復雜。一旦出現(xiàn)錯誤,比python更難調(diào)試。代碼已經(jīng)盡可能優(yōu)化了,但還是遠遠超過Python。