pandas操作excel文件的小技巧

pandas讀取excel設置第一列爲序號

設置參數index_col=0可以設置讀取excel時第一列爲序號,否則會自動添加一列序號從0開始:

data = pd.read_excel(file_path, index_col=0)

pandas讀取excel設置第一行爲列名

默認參數 header=0 會設置讀取的excel第一行爲列名, header=None會新建一行從0開始的序號列名:

data = pd.read_excel(file_path, header=0)

pandas讀取excel設置序號從1開始

all = pd.concat(all_data)
all.index = range(1, len(all) + 1)

pandas合併excel重編序列號

all_data是series,dataframe或者是panel構成的序列list,設置 ignore_index=True會重編序號:

all = pd.concat(all_data, ignore_index=True)

pandas保存excel不存儲序號

設置index=None存儲爲excel不會存儲序號:

data.to_excel(file_path, index=None)

pandas讀取excel數字會掉前面0的問題

加入dtype=object可以設置數字格式以文本顯示,數字前面的0不會丟掉:

data = pd.read_excel(file_path, dtype=object)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章