Pandas基礎(chǔ)命令速查表

最近用到Pandas,想查幾個(gè)函數(shù),結(jié)果翻來翻去都是那幾個(gè)豆腐干大小的圖片,我真是緩緩地打出一個(gè)?難道你能對(duì)著一張圖片ctrl + F5?

于是趕緊安排一下官方庫(kù)的翻譯。這不,老鐵,來了~


本文翻譯整理自Pandas Cheat Sheet - Python for DataScience

點(diǎn)這里一鍵fork >>Pandas基礎(chǔ)命令速查清單

速查表內(nèi)容概要

[縮寫解釋 & 庫(kù)的導(dǎo)入]

[數(shù)據(jù)的導(dǎo)入]

[數(shù)據(jù)的導(dǎo)出]

[創(chuàng)建測(cè)試對(duì)象]

[數(shù)據(jù)的查看與檢查]

[數(shù)據(jù)的選取]

[數(shù)據(jù)的清洗]

[數(shù)據(jù)的過濾(filter),排序(sort)和分組(groupby)]

[數(shù)據(jù)的連接(join)與組合(combine)]

[數(shù)據(jù)的統(tǒng)計(jì)]

縮寫解釋 & 庫(kù)的導(dǎo)入

df --- 任意的pandas DataFrame(數(shù)據(jù)框)對(duì)象

s --- 任意的pandas Series(數(shù)組)對(duì)象

pandas和numpy是用Python做數(shù)據(jù)分析最基礎(chǔ)且最核心的庫(kù)

import pandas as pd # 導(dǎo)入pandas庫(kù)并簡(jiǎn)寫為pd

import numpy as np # 導(dǎo)入numpy庫(kù)并簡(jiǎn)寫為np

數(shù)據(jù)的導(dǎo)入

pd.read_csv(filename) # 導(dǎo)入csv格式文件中的數(shù)據(jù)

pd.read_table(filename) # 導(dǎo)入有分隔符的文本 (如TSV) 中的數(shù)據(jù)

pd.read_excel(filename) # 導(dǎo)入Excel格式文件中的數(shù)據(jù)

pd.read_sql(query, connection_object) # 導(dǎo)入SQL數(shù)據(jù)表/數(shù)據(jù)庫(kù)中的數(shù)據(jù)

pd.read_json(json_string) # 導(dǎo)入JSON格式的字符,URL地址或者文件中的數(shù)據(jù)

pd.read_html(url) # 導(dǎo)入經(jīng)過解析的URL地址中包含的數(shù)據(jù)框 (DataFrame) 數(shù)據(jù)

pd.read_clipboard() # 導(dǎo)入系統(tǒng)粘貼板里面的數(shù)據(jù)

pd.DataFrame(dict) # 導(dǎo)入Python字典 (dict) 里面的數(shù)據(jù),其中key是數(shù)據(jù)框的表頭,value是數(shù)據(jù)框的內(nèi)容。

數(shù)據(jù)的導(dǎo)出

df.to_csv(filename) # 將數(shù)據(jù)框 (DataFrame)中的數(shù)據(jù)導(dǎo)入csv格式的文件中

df.to_excel(filename) # 將數(shù)據(jù)框 (DataFrame)中的數(shù)據(jù)導(dǎo)入Excel格式的文件中

df.to_sql(table_name,connection_object) # 將數(shù)據(jù)框 (DataFrame)中的數(shù)據(jù)導(dǎo)入SQL數(shù)據(jù)表/數(shù)據(jù)庫(kù)中

df.to_json(filename) # 將數(shù)據(jù)框 (DataFrame)中的數(shù)據(jù)導(dǎo)入JSON格式的文件中

創(chuàng)建測(cè)試對(duì)象

pd.DataFrame(np.random.rand(10,5)) # 創(chuàng)建一個(gè)5列10行的由隨機(jī)浮點(diǎn)數(shù)組成的數(shù)據(jù)框

DataFrame

pd.DataFrame(np.random.rand(10,5))

pd.Series(my_list) # 從一個(gè)可迭代的對(duì)象 my_list 中創(chuàng)建一個(gè)數(shù)據(jù)組

my_list = ['Kesci',100,'歡迎來到科賽網(wǎng)']

pd.Series(my_list)

df.index = pd.date_range('2017/1/1',periods=df.shape[0]) # 添加一個(gè)日期索引 index

df = pd.DataFrame(np.random.rand(10,5))

df.index = pd.date_range('2017/1/1',periods=df.shape[0])

df

數(shù)據(jù)的查看與檢查

df.head(n) # 查看數(shù)據(jù)框的前n行

df = pd.DataFrame(np.random.rand(10,5))

df.head(3)

df.tail(n) # 查看數(shù)據(jù)框的最后n行

df = pd.DataFrame(np.random.rand(10,5))

df.tail(3)

df.shape # 查看數(shù)據(jù)框的行數(shù)與列數(shù)

df = pd.DataFrame(np.random.rand(10,5))

df.shape

df.info() # 查看數(shù)據(jù)框(DataFrame) 的索引、數(shù)據(jù)類型及內(nèi)存信息

df = pd.DataFrame(np.random.rand(10,5))

df.info()

df.describe() # 對(duì)于數(shù)據(jù)類型為數(shù)值型的列,查詢其描述性統(tǒng)計(jì)的內(nèi)容

df.describe()

s.value_counts(dropna=False) # 查詢每個(gè)獨(dú)特?cái)?shù)據(jù)值出現(xiàn)次數(shù)統(tǒng)計(jì)

s = pd.Series([1,2,3,3,4,np.nan,5,5,5,6,7])

s.value_counts(dropna=False)

df.apply(pd.Series.value_counts) # 查詢數(shù)據(jù)框 (Data Frame) 中每個(gè)列的獨(dú)特?cái)?shù)據(jù)值出現(xiàn)次數(shù)統(tǒng)計(jì)

數(shù)據(jù)的選取

df[col] # 以數(shù)組Series 的形式返回選取的列

df =pd.DataFrame(np.random.rand(5,5),columns=list('ABCDE'))

df['C']

df[[col1, col2]] # 以新的數(shù)據(jù)框(DataFrame)的形式返回選取的列

df =pd.DataFrame(np.random.rand(5,5),columns=list('ABCDE'))

df[['B','E']]

s.iloc[0] # 按照位置選取

s =pd.Series(np.array(['I','Love','Data']))

s.iloc[0]

s.loc['index_one'] # 按照索引選取

s =pd.Series(np.array(['I','Love','Data']))

s.loc[1]

df.iloc[0,:] # 選取第一行

df =pd.DataFrame(np.random.rand(5,5),columns=list('ABCDE'))

df.iloc[0,:]

df.iloc[0,0] # 選取第一行的第一個(gè)元素

df = pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.iloc[0,0]

數(shù)據(jù)的清洗

df.columns = ['a','b'] # 重命名數(shù)據(jù)框的列名稱

df =pd.DataFrame({'A':np.array([1,np.nan,2,3,6,np.nan]),

????????????????????????????????'B':np.array([np.nan,4,np.nan,5,9,np.nan]),

????????????????????????????????'C':'foo'})

df.columns = ['a','b','c']

df

pd.isnull() # 檢查數(shù)據(jù)中空值出現(xiàn)的情況,并返回一個(gè)由布爾值(True,Fale)組成的列

df =pd.DataFrame({'A':np.array([1,np.nan,2,3,6,np.nan]),

????????????????????????????????'B':np.array([np.nan,4,np.nan,5,9,np.nan]),

????????????????????????????????'C':'foo'})

pd.isnull(df)

pd.notnull() # 檢查數(shù)據(jù)中非空值出現(xiàn)的情況,并返回一個(gè)由布爾值(True,False)組成的列

df =pd.DataFrame({'A':np.array([1,np.nan,2,3,6,np.nan]),

????????????????????????????????'B':np.array([np.nan,4,np.nan,5,9,np.nan]),

????????????????????????????????'C':'foo'})

pd.notnull(df)

df.dropna() # 移除數(shù)據(jù)框 DataFrame 中包含空值的行

df =pd.DataFrame({'A':np.array([1,np.nan,2,3,6,np.nan]),

????????????????????????????????'B':np.array([np.nan,4,np.nan,5,9,np.nan]),

????????????????????????????????'C':'foo'})

df.dropna()

df.dropna(axis=1) # 移除數(shù)據(jù)框 DataFrame 中包含空值的列

df =pd.DataFrame({'A':np.array([1,np.nan,2,3,6,np.nan]),

????????????????????????????????'B':np.array([np.nan,4,np.nan,5,9,np.nan]),

????????????????????????????????'C':'foo'})

df.dropna(axis=1)

df.dropna(axis=1,thresh=n) # 移除數(shù)據(jù)框df中空值個(gè)數(shù)不超過n的行

df =pd.DataFrame({'A':np.array([1,np.nan,2,3,6,np.nan]),

????????????????????????????????'B':np.array([np.nan,4,np.nan,5,9,np.nan]),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'C':'foo'})

test = df.dropna(axis=1,thresh=1)

test

df.fillna(x) # 將數(shù)據(jù)框 DataFrame 中的所有空值替換為 x

df =pd.DataFrame({'A':np.array([1,np.nan,2,3,6,np.nan]),

????????????????????????????????'B':np.array([np.nan,4,np.nan,5,9,np.nan]),

????????????????????????????????'C':'foo'})

df.fillna('Test')

s.fillna(s.mean()) -> 將所有空值替換為平均值

s = pd.Series([1,3,5,np.nan,7,9,9])

s.fillna(s.mean())

s.astype(float) # 將數(shù)組(Series)的格式轉(zhuǎn)化為浮點(diǎn)數(shù)

s = pd.Series([1,3,5,np.nan,7,9,9])

s.astype(float)

s.replace(1,'one') # 將數(shù)組(Series)中的所有1替換為'one'

s = pd.Series([1,3,5,np.nan,7,9,9])

s.replace(1,'one')

s.replace([1,3],['one','three']) # 將數(shù)組(Series)中所有的1替換為'one',所有的3替換為'three'

s = pd.Series([1,3,5,np.nan,7,9,9])

s.replace([1,3],['one','three'])

df.rename(columns=lambda x: x + 2) # 將全體列重命名

df = pd.DataFrame(np.random.rand(4,4))

df.rename(columns=lambda x: x+ 2)

df.rename(columns={'old_name': 'new_name'}) # 將選擇的列重命名

df =pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.rename(columns={'A':'newA','C':'newC'})

df.set_index('column_one') # 改變索引

df =pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.set_index('B')

df.rename(index = lambda x: x+ 1) # 改變?nèi)w索引

df = pd.DataFrame(np.random.rand(10,5))

df.rename(index = lambda x: x+ 1)

數(shù)據(jù)的過濾(filter),排序(sort)和分組(groupby)

df[df[col] > 0.5] # 選取數(shù)據(jù)框df中對(duì)應(yīng)行的數(shù)值大于0.5的全部列

df =pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df[df['A'] > 0.5]

df[(df[col] > 0.5) & (df[col] <0.7)] # 選取數(shù)據(jù)框df中對(duì)應(yīng)行的數(shù)值大于0.5,并且小于0.7的全部列

df =pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df[(df['C'] > 0.5) & (df['D'] <0.7)]

df.sort_values(col1) # 按照數(shù)據(jù)框的列col1升序(ascending)的方式對(duì)數(shù)據(jù)框df做排序

df =pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.sort_values('E')

df.sort_values(col2,ascending=False) # 按照數(shù)據(jù)框的列col2降序(descending)的方式對(duì)數(shù)據(jù)框df做排序

df =pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.sort_values('A',ascending=False)

df.sort_values([col1,col2],ascending=[True,False]) # 按照數(shù)據(jù)框的列col1升序,col2降序的方式對(duì)數(shù)據(jù)框df做排序

df =pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.sort_values(['A','E'],ascending=[True,False])

df.groupby(col) # 按照某列對(duì)數(shù)據(jù)框df做分組

df = pd.DataFrame({'A':np.array(['foo','foo','foo','foo','bar','bar']),

????????????????????????????????'B':np.array(['one','one','two','two','three','three']),

????????????????????????????????'C':np.array(['small','medium','large','large','small','small']),

????????????????????????????????'D':np.array([1,2,2,3,3,5])})

df.groupby('A').count()

df.groupby([col1,col2]) # 按照列col1和col2對(duì)數(shù)據(jù)框df做分組

df =pd.DataFrame({'A':np.array(['foo','foo','foo','foo','bar','bar']),

????????????????????????????????'B':np.array(['one','one','two','two','three','three']),

????????????????????????????????'C':np.array(['small','medium','large','large','small','small']),

????????????????????????????????'D':np.array([1,2,2,3,3,5])})

df.groupby(['B','C']).sum()

df.groupby(col1)[col2].mean() # 按照列col1對(duì)數(shù)據(jù)框df做分組處理后,返回對(duì)應(yīng)的col2的平均值

df =pd.DataFrame({'A':np.array(['foo','foo','foo','foo','bar','bar']),

????????????????????????????????'B':np.array(['one','one','two','two','three','three']),

????????????????????????????????'C':np.array(['small','medium','large','large','small','small']),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'D':np.array([1,2,2,3,3,5])})

df.groupby('B')['D'].mean()

df.pivot_table(index=col1,values=[col2,col3],aggfunc=mean)

# 做透視表,索引為col1,針對(duì)的數(shù)值列為col2和col3,分組函數(shù)為平均值

df =pd.DataFrame({'A':np.array(['foo','foo','foo','foo','bar','bar']),

????????????????????????????????'B':np.array(['one','one','two','two','three','three']),

????????????????????????????????'C':np.array(['small','medium','large','large','small','small']),

????????????????????????????????'D':np.array([1,2,2,3,3,5])})

df.pivot_table(df,index=['A','B'],columns=['C'],aggfunc=np.sum)

df.groupby(col1).agg(np.mean) #?返回按列col1分組的所有列的均值

df =pd.DataFrame({'A':np.array(['foo','foo','foo','foo','bar','bar']),

????????????????????????????????'B':np.array(['one','one','two','two','three','three']),

????????????????????????????????'C':np.array(['small','medium','large','large','small','small']),

????????????????????????????????'D':np.array([1,2,2,3,3,5])})

df.groupby('A').agg(np.mean)

df.apply(np.mean) # 對(duì)數(shù)據(jù)框df的每一列求平均值

df = pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.apply(np.mean)

df.apply(np.max,axis=1) # 對(duì)數(shù)據(jù)框df的每一行求最大值

df =pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.apply(np.max,axis=1)

數(shù)據(jù)的連接(join)與組合(combine)

df1.append(df2) # 在數(shù)據(jù)框df2的末尾添加數(shù)據(jù)框df1,其中df1和df2的列數(shù)應(yīng)該相等

df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2','A3'],

????????????????????????????????????'B': ['B0', 'B1', 'B2','B3'],

????????????????????????????????????'C': ['C0', 'C1', 'C2','C3'],

????????????????????????????????????'D': ['D0', 'D1', 'D2','D3']},index=[0, 1, 2, 3])

df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6','A7'],

????????????????????????????????????'B': ['B4', 'B5', 'B6','B7'],

????????????????????????????????????'C': ['C4', 'C5', 'C6','C7'],

????????????????????????????????????'D': ['D4', 'D5', 'D6','D7']},index=[4, 5, 6, 7])

df1.append(df2)

pd.concat([df1, df2],axis=1) # 在數(shù)據(jù)框df1的列最后添加數(shù)據(jù)框df2,其中df1和df2的行數(shù)應(yīng)該相等

df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2','A3'],

????????????????????????????????????'B': ['B0', 'B1', 'B2','B3'],

????????????????????????????????????'C': ['C0', 'C1', 'C2','C3'],

????????????????????????????????????'D': ['D0', 'D1', 'D2','D3']},index=[0, 1, 2, 3])

df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6','A7'],

????????????????????????????????????'B': ['B4', 'B5', 'B6','B7'],

????????????????????????????????????'C': ['C4', 'C5', 'C6','C7'],

????????????????????????????????????'D': ['D4', 'D5', 'D6','D7']},index=[4, 5, 6, 7])

pd.concat([df1,df2],axis=1)

df1.join(df2,on=col1,how='inner') # 對(duì)數(shù)據(jù)框df1和df2做內(nèi)連接,其中連接的列為col1

df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2','A3'],

???????????????????????????????????'B': ['B0', 'B1', 'B2','B3'],

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'key': ['K0', 'K1', 'K0','K1']})

df2 = pd.DataFrame({'C': ['C0', 'C1'],

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'D': ['D0', 'D1']},index=['K0', 'K1'])

df1.join(df2, on='key')

數(shù)據(jù)的統(tǒng)計(jì)

df.describe() # 得到數(shù)據(jù)框df每一列的描述性統(tǒng)計(jì)

df =pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.describe()

df.mean() # 得到數(shù)據(jù)框df中每一列的平均值

df = pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.mean()

df.corr() # 得到數(shù)據(jù)框df中每一列與其他列的相關(guān)系數(shù)

df =pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.corr()

df.count() # 得到數(shù)據(jù)框df中每一列的非空值個(gè)數(shù)

df =pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.count()

df.max() # 得到數(shù)據(jù)框df中每一列的最大值

df =pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.max()

df.min() # 得到數(shù)據(jù)框df中每一列的最小值

df = pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.min()

df.median() # 得到數(shù)據(jù)框df中每一列的中位數(shù)

df =pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.median()

df.std() # 得到數(shù)據(jù)框df中每一列的標(biāo)準(zhǔn)差

df =pd.DataFrame(np.random.rand(10,5),columns=list('ABCDE'))

df.std()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容