Pandas常見用法總結

2021-09-13 10:11:39 字數 4867 閱讀 2326

在這個速查手冊中,我們使用如下縮寫:

df:任意的pandas dataframe物件

s:任意的pandas series物件

raw:行標籤

col:列標籤

引入響應模組:

import pandasaspd

import numpyasnp

pd.read_excel(io, sheetname=0, header=0, skiprows=none, skip_footer=0, index_col=none, names=none, parse_cols=none, parse_dates=false, date_parser=none, na_values=none, thousands=none, convert_float=true, has_index_names=none, converters=none, dtype=none, true_values=none, false_values=none, engine=none, squeeze=false,**kwds)

'''該函式主要的引數為:io、sheetname、header、names、encoding。

io:excel檔案,可以是檔案路徑、檔案**、file-like物件、xlrd workbook;

sheetname:返回指定的sheet,引數可以是字串(sheet名)、整型(sheet索引)、list(元素為字串和整型,返回字典)、none(返回字典,全部sheet);(如果乙個excel含有多個表,如果你只想讀入其中乙個可以:df = pd.read_excel('log.xls', sheetname=1))

header:指定資料表的表頭,引數可以是int、list of ints,即為索引行數為表頭;

names:返回指定name的列,引數為array-like物件。

encoding:關鍵字引數,指定以何種編碼讀取。

該函式返回pandas中的dataframe或dict of dataframe物件,利用dataframe的相關操作即可讀取相應的資料。'''

pd.read_table(r'e:\test.txt', sep=' ',index_col=0)

'''常用引數:

header=none:沒有每列的column name,可以自己設定

encoding='gb2312':其他編碼中文顯示錯誤

index_col=0:設定第1列資料作為index

sep:設定分隔符'''

dataframe.to_excel(excel_writer, sheet_name='sheet1', na_rep='', float_format=none, columns=none, header=true, index=true, index_label=none, startrow=0, startcol=0, engine=none, merge_cells=true, encoding=none, inf_rep='inf', verbose=true, freeze_panes=none)

該函式主要引數為:excel_writer。

excel_writer:寫入的目標excel檔案,可以是檔案路徑、excelwriter物件;

sheet_name:被寫入的sheet名稱,string型別,預設為'sheet1';

na_rep:缺失值表示,string型別;

header:是否寫表頭資訊,布林或listof string型別,預設為true;

index:是否寫行號,布林型別,預設為true;

encoding:指定寫入編碼,string型別。

注:loc函式根據行標籤進行行選擇;

iloc函式根據行位置進行行選擇;

ix函式可以根據行位置選擇也可以根據行標籤選擇。

注:這個drop_duplicate方法是對dataframe格式的資料,去除特定列下面的重複行。返回dataframe格式的資料。

df.concat([df1, df2],axis=1):將df2中的列新增到df1的尾部

df1.join(df2,on=col1,how='inner'):對df1的列和df2的列執行sql形式的join

附加:

1.字典內嵌列表

importpandas as pd

data=

df=pd.dataframe(data)

printdf

輸出:

age      name

028tom

134jack

229steve

342ricky

2.列表內嵌字典

列表內嵌字典,字典的鍵預設為列名。

importpandas as pd

data=[,]

df=pd.dataframe(data)

printdf

輸出:

a    b      c

012nan

151020.0

常見命令用法總結

目錄 1 關於grep的總結 2 關於egrep的總結 3 關於tr的總結 4 關於sort的總結 5 關於uniq的總結 6 關於cut的總結 7 關於tee的總結 8 關於split的總結 9 關於type的使用 10 關於find的使用 11 關於xargs的使用 關於grep的總結 找出所需...

Android Intent 常見用法總結

android intent 常見用法總結 1.開啟網頁 uri uri uri.parse intent intent new intent intent.action view,uri startactivity intent 2.開啟地圖 必須使用google apis包 uri uri ur...

Android Intent 常見用法總結

android intent 常見用法總結 1.開啟網頁 uri uri uri.parse intent intent new intent intent.action view,uri startactivity intent 2.開啟地圖 必須使用google apis包 uri uri ur...