Pandas處理excel自動化常用操作

2021-10-08 10:30:04 字數 2576 閱讀 8568

import pandas

pandas.read_excel(io,sheet_name = 0,header = 0,names = none,index_col = none,usecols = none,squeeze = false,dtype = none, ...)

行列index從0開始

io:字串,檔案的路徑物件。

sheet_name:none、string、int、字串列表或整數列表,預設為0。字串用於工作表名稱,整數用於零索引工作表位置,字串列表或整數列表用於請求多個工作表,為none時獲取所有工作表。

值對應操作

sheet_name=0

第一張作為dataframe

sheet_name=1

第二張作為dataframe

sheet_name=「sheet1」

第一張作dataframe

sheet_name=[0,1,'sheet5']

第1頁,第2頁和第5頁作為dataframes的字典。

header:指定作為列名的行,預設0,即取第一行的值為列名。資料為列名行以下的資料;若資料不含列名,則設定 header = none。

names:預設為none,要使用的列名列表,如不包含標題行,應顯示傳遞header=none

index_col:指定列為索引列,預設none列(0索引)用作dataframe的行標籤。

usecols:list,預設為none。

squeeze:boolean,預設為false,如果解析的資料只包含一列,則返回乙個series。

dtype:列的型別名稱或字典,預設為none。資料或列的資料型別。例如使用物件儲存儲存在excel中的資料而不解釋dtype。如果指定了轉換器,則它們將應用於dtype轉換的instead。

skiprows: 需要忽略的行數(從檔案開始處算起),或需要跳過的行號列表(從0開始)

skipfooter: int, default 0 從檔案尾部開始忽略。

head(): 取去除header的前5行資料

tail(): 取後5行資料

df["列名"]: 按列名取列資料

df[["列名1","列名2"]]: 按列名取列資料

df[0:3]: 取前三行資料,按行非index

df[-3:]: 取後三行資料,按行非index

df.列名1:獲取列名1的資料

df.iloc[[1],[2]].values: 取單元格每二行,第三列的值,返回格式[[value]],從0起

df.iloc[i].values:返回第i+1行的資料,從0起

df.iloc[1:3].values:返回第二至四條資料,1,3為index,從0起

df.iloc[:,[1,2]]::表示所有行,返回第2,3列資料,從0起

df.iloc[0:3,3:]:表示索引0-3行,返回第4列與之後列的資料,從0起

df.loc[1].values:返回index為1的行

df.loc[1:3].values:返回第二至四條資料,1,3為index

df.loc[:,["列名1","列名2"]]::表示所有行,返回列資料

df.loc[0:3,"列名1":]:表示索引0-3行,返回列名1與之後列的資料

df.columns.get_loc("列名"):返回列索引值

df.pop("列名"):刪除列

del df["列名"]:刪除列

pd.dataframe(data,index,columns,dtype)

data: 可以為列表,如:["a","b","c"] ,[["abc",123],["dsf",343],["dkfs",90]]

可以為字典,如:

data=
或:

data=
username,age作為column

index: 可以為列表

columns:可以為列表

df.drop(0):刪除一行,0為index

df.reindex(index=[0,1,2],columns=["a","b","c"]):重建索引,如索引已存在使用原索引,不存在則建立

df.rename(index=,columns=:重新命名索引

按列列印整張表資料:

for key,value in df.iteritems():

print(key)

print(value)

按行列印整張表資料:

for key,value in df.iterrows():

print(key)

print(value)

按元組方式獲取行資料:

for row in df.itertuples():

print(row)

pandas處理excel資料

import pandas as pd f pd.read excel name.xlsx sheet name 檢視有重複的行 re row f.duplicated 去除重複行資料 drop duplicates subset 列名 keep firsrt inplace true 函式是刪除d...

Excel檔案處理之pandas

pandas可以用來處理多種儲存型別的資料,比如excel csv html sql.檢視pandas.read xx方法可以看出。本文主要總結對excel的處理。pandas依賴處理excel的xlrd等模組,這些模組安裝即可,不需要import。1讀取 1.1多個sheet import pan...

乾貨 利用pandas處理Excel資料

新建乙個excel table1.csv 用於案例講解 df pd.read excel table1.xlsx 相對路徑 df pd.read excel r e anaconda hc datascience table1.csv 絕對路徑 顯示資料的行與列數 df.shape 6,5 顯示資料...