柚子快報(bào)邀請(qǐng)碼778899分享:Pandas教程(非常詳細(xì))
柚子快報(bào)邀請(qǐng)碼778899分享:Pandas教程(非常詳細(xì))
print(s)
輸出結(jié)果:
100 a 101 b 102 c 103 d dtype: object
3) dict創(chuàng)建Series對(duì)象
您可以把 dict 作為輸入數(shù)據(jù)。如果沒有傳入索引時(shí)會(huì)按照字典的鍵來構(gòu)造索引;反之,當(dāng)傳遞了索引時(shí)需要將索引標(biāo)簽與字典中的值一一對(duì)應(yīng)。
下面兩組示例分別對(duì)上述兩種情況做了演示。
示例1,沒有傳遞索引時(shí):
import pandas as pd import numpy as np data = {‘a(chǎn)’ : 0., ‘b’ : 1., ‘c’ : 2.} s = pd.Series(data) print(s)
輸出結(jié)果:
a 0.0 b 1.0 c 2.0 dtype: float64
示例 2,為index參數(shù)傳遞索引時(shí):
import pandas as pd import numpy as np data = {‘a(chǎn)’ : 0., ‘b’ : 1., ‘c’ : 2.} s = pd.Series(data,index=[‘b’,‘c’,‘d’,‘a(chǎn)’]) print(s)
輸出結(jié)果:
b 1.0 c 2.0 d NaN a 0.0 dtype: float64
當(dāng)傳遞的索引值無法找到與其對(duì)應(yīng)的值時(shí),使用 NaN(非數(shù)字)填充。
4) 標(biāo)量創(chuàng)建Series對(duì)象
如果 data 是標(biāo)量值,則必須提供索引,示例如下:
import pandas as pd import numpy as np s = pd.Series(5, index=[0, 1, 2, 3]) print(s)
輸出如下:
0 5 1 5 2 5 3 5 dtype: int64
標(biāo)量值按照 index 的數(shù)量進(jìn)行重復(fù),并與其一一對(duì)應(yīng)。
訪問Series數(shù)據(jù)
上述講解了創(chuàng)建 Series 對(duì)象的多種方式,那么我們應(yīng)該如何訪問 Series 序列中元素呢?分為兩種方式,一種是位置索引訪問;另一種是索引標(biāo)簽訪問。
1) 位置索引訪問
這種訪問方式與 ndarray 和 list 相同,使用元素自身的下標(biāo)進(jìn)行訪問。我們知道數(shù)組的索引計(jì)數(shù)從 0 開始,這表示第一個(gè)元素存儲(chǔ)在第 0 個(gè)索引位置上,以此類推,就可以獲得 Series 序列中的每個(gè)元素。下面看一組簡(jiǎn)單的示例:
import pandas as pd s = pd.Series([1,2,3,4,5],index = [‘a(chǎn)’,‘b’,‘c’,‘d’,‘e’]) print(s[0]) #位置下標(biāo) print(s[‘a(chǎn)’]) #標(biāo)簽下標(biāo)
輸出結(jié)果:
1 1
通過切片的方式訪問 Series 序列中的數(shù)據(jù),示例如下:
import pandas as pd s = pd.Series([1,2,3,4,5],index = [‘a(chǎn)’,‘b’,‘c’,‘d’,‘e’]) print(s[:3])
輸出結(jié)果:
a 1 b 2 c 3 dtype: int64
如果想要獲取最后三個(gè)元素,也可以使用下面的方式:
import pandas as pd s = pd.Series([1,2,3,4,5],index = [‘a(chǎn)’,‘b’,‘c’,‘d’,‘e’]) print(s[-3:])
輸出結(jié)果:
c 3 d 4 e 5 dtype: int64
2) 索引標(biāo)簽訪問
Series 類似于固定大小的 dict,把 index 中的索引標(biāo)簽當(dāng)做 key,而把 Series 序列中的元素值當(dāng)做 value,然后通過 index 索引標(biāo)簽來訪問或者修改元素值。
示例1,使用索標(biāo)簽訪問單個(gè)元素值:
import pandas as pd s = pd.Series([6,7,8,9,10],index = [‘a(chǎn)’,‘b’,‘c’,‘d’,‘e’]) print(s[‘a(chǎn)’])
輸出結(jié)果:
6
示例 2,使用索引標(biāo)簽訪問多個(gè)元素值
import pandas as pd s = pd.Series([6,7,8,9,10],index = [‘a(chǎn)’,‘b’,‘c’,‘d’,‘e’]) print(s[[‘a(chǎn)’,‘c’,‘d’]])
輸出結(jié)果:
a 6 c 8 d 9 dtype: int64
示例3,如果使用了 index 中不包含的標(biāo)簽,則會(huì)觸發(fā)異常:
import pandas as pd s = pd.Series([6,7,8,9,10],index = [‘a(chǎn)’,‘b’,‘c’,‘d’,‘e’]) #不包含f值 print(s[‘f’])
輸出結(jié)果:
… KeyError: ‘f’
Series常用屬性
下面我們介紹 Series 的常用屬性和方法。在下表列出了 Series 對(duì)象的常用屬性。
名稱屬性axes以列表的形式返回所有行索引標(biāo)簽。dtype返回對(duì)象的數(shù)據(jù)類型。empty返回一個(gè)空的 Series 對(duì)象。ndim返回輸入數(shù)據(jù)的維數(shù)。size返回輸入數(shù)據(jù)的元素?cái)?shù)量。values以 ndarray 的形式返回 Series 對(duì)象。index返回一個(gè)RangeIndex對(duì)象,用來描述索引的取值范圍。
現(xiàn)在創(chuàng)建一個(gè) Series 對(duì)象,并演示如何使用上述表格中的屬性。如下所示:
import pandas as pd import numpy as np s = pd.Series(np.random.randn(5)) print(s)
輸出結(jié)果:
0 0.898097 1 0.730210 2 2.307401 3 -1.723065 4 0.346728 dtype: float64
上述示例的行索引標(biāo)簽是 [0,1,2,3,4]。
1) axes
import pandas as pd import numpy as np s = pd.Series(np.random.randn(5)) print (“The axes are:”) print(s.axes)
輸出結(jié)果
The axes are: [RangeIndex(start=0, stop=5, step=1)]
2) dtype
import pandas as pd import numpy as np s = pd.Series(np.random.randn(5)) print (“The dtype is:”) print(s.dtype)
輸出結(jié)果:
The dtype is: float64
3) empty
返回一個(gè)布爾值,用于判斷數(shù)據(jù)對(duì)象是否為空。示例如下:
import pandas as pd import numpy as np s = pd.Series(np.random.randn(5)) print(“是否為空對(duì)象?”) print (s.empty)
輸出結(jié)果:
是否為空對(duì)象? False
4) ndim
查看序列的維數(shù)。根據(jù)定義,Series 是一維數(shù)據(jù)結(jié)構(gòu),因此它始終返回 1。
import pandas as pd import numpy as np s = pd.Series(np.random.randn(5)) print (s) print (s.ndim)
輸出結(jié)果:
0 0.311485 1 1.748860 2 -0.022721 3 -0.129223 4 -0.489824 dtype: float64 1
5) size
返回 Series 對(duì)象的大小(長(zhǎng)度)。
import pandas as pd import numpy as np s = pd.Series(np.random.randn(3)) print (s) #series的長(zhǎng)度大小 print(s.size)
輸出結(jié)果:
0 -1.866261 1 -0.636726 2 0.586037 dtype: float64 3
6) values
以數(shù)組的形式返回 Series 對(duì)象中的數(shù)據(jù)。
import pandas as pd import numpy as np s = pd.Series(np.random.randn(6)) print(s) print(“輸出series中數(shù)據(jù)”) print(s.values)
輸出結(jié)果:
0 -0.502100 1 0.696194 2 -0.982063 3 0.416430 4 -1.384514 5 0.444303 dtype: float64 輸出series中數(shù)據(jù) [-0.50210028 0.69619407 -0.98206327 0.41642976 -1.38451433 0.44430257]
7) index
該屬性用來查看 Series 中索引的取值范圍。示例如下:
#顯示索引 import pandas as pd s=pd.Series([1,2,5,8],index=[‘a(chǎn)’,‘b’,‘c’,‘d’]) print(s.index) #隱式索引 s1=pd.Series([1,2,5,8]) print(s1.index)
輸出結(jié)果:
隱式索引: Index([‘a(chǎn)’, ‘b’, ‘c’, ‘d’], dtype=‘object’) 顯示索引: RangeIndex(start=0, stop=4, step=1)
Series常用方法
1) head()&tail()查看數(shù)據(jù)
如果想要查看 Series 的某一部分?jǐn)?shù)據(jù),可以使用 head() 或者 tail() 方法。其中 head() 返回前 n 行數(shù)據(jù),默認(rèn)顯示前 5 行數(shù)據(jù)。示例如下:
import pandas as pd import numpy as np s = pd.Series(np.random.randn(5)) print (“The original series is:”) print (s) #返回前三行數(shù)據(jù) print (s.head(3))
輸出結(jié)果:
原系列輸出結(jié)果: 0 1.249679 1 0.636487 2 -0.987621 3 0.999613 4 1.607751 head(3)輸出: dtype: float64 0 1.249679 1 0.636487 2 -0.987621 dtype: float64
tail() 返回的是后 n 行數(shù)據(jù),默認(rèn)為后 5 行。示例如下:
import pandas as pd import numpy as np s = pd.Series(np.random.randn(4)) #原series print(s) #輸出后兩行數(shù)據(jù) print (s.tail(2))
輸出結(jié)果:
原Series輸出: 0 0.053340 1 2.165836 2 -0.719175 3 -0.035178 輸出后兩行數(shù)據(jù): dtype: float64 2 -0.719175 3 -0.035178 dtype: float64
2) isnull()&nonull()檢測(cè)缺失值
isnull() 和 nonull() 用于檢測(cè) Series 中的缺失值。所謂缺失值,顧名思義就是值不存在、丟失、缺少。
isnull():如果為值不存在或者缺失,則返回 True。notnull():如果值不存在或者缺失,則返回 False。
其實(shí)不難理解,在實(shí)際的數(shù)據(jù)分析任物中,數(shù)據(jù)的收集往往要經(jīng)歷一個(gè)繁瑣的過程。在這個(gè)過程中難免會(huì)因?yàn)橐恍┎豢煽沽?,或者人為因素?dǎo)致數(shù)據(jù)丟失的現(xiàn)象。這時(shí),我們可以使用相應(yīng)的方法對(duì)缺失值進(jìn)行處理,比如均值插值、數(shù)據(jù)補(bǔ)齊等方法。上述兩個(gè)方法就是幫助我們檢測(cè)是否存在缺失值。示例如下:
import pandas as pd #None代表缺失數(shù)據(jù) s=pd.Series([1,2,5,None]) print(pd.isnull(s)) #是空值返回True print(pd.notnull(s)) #空值返回False
輸出結(jié)果:
0 False 1 False 2 False 3 True dtype: bool
notnull(): 0 True 1 True 2 True 3 False dtype: bool
Pandas DataFrame入門教程(圖解版)
DataFrame 是 Pandas 的重要數(shù)據(jù)結(jié)構(gòu)之一,也是在使用 Pandas 進(jìn)行數(shù)據(jù)分析過程中最常用的結(jié)構(gòu)之一,可以這么說,掌握了 DataFrame 的用法,你就擁有了學(xué)習(xí)數(shù)據(jù)分析的基本能力。
認(rèn)識(shí)DataFrame結(jié)構(gòu)
DataFrame 一個(gè)表格型的數(shù)據(jù)結(jié)構(gòu),既有行標(biāo)簽(index),又有列標(biāo)簽(columns),它也被稱異構(gòu)數(shù)據(jù)表,所謂異構(gòu),指的是表格中每列的數(shù)據(jù)類型可以不同,比如可以是字符串、整型或者浮點(diǎn)型等。其結(jié)構(gòu)圖示意圖,如下所示:
表格中展示了某個(gè)銷售團(tuán)隊(duì)個(gè)人信息和績(jī)效評(píng)級(jí)(rating)的相關(guān)數(shù)據(jù)。數(shù)據(jù)以行和列形式來表示,其中每一列表示一個(gè)屬性,而每一行表示一個(gè)條目的信息。
下表展示了上述表格中每一列標(biāo)簽所描述數(shù)據(jù)的數(shù)據(jù)類型,如下所示:
ColumnTypenameStringageintegergenderStringratingFloat
DataFrame 的每一行數(shù)據(jù)都可以看成一個(gè) Series 結(jié)構(gòu),只不過,DataFrame 為這些行中每個(gè)數(shù)據(jù)值增加了一個(gè)列標(biāo)簽。因此 DataFrame 其實(shí)是從 Series 的基礎(chǔ)上演變而來。在數(shù)據(jù)分析任務(wù)中 DataFrame 的應(yīng)用非常廣泛,因?yàn)樗枋鰯?shù)據(jù)的更為清晰、直觀。
通過示例對(duì) DataFrame 結(jié)構(gòu)做進(jìn)一步講解。 下面展示了一張學(xué)生成績(jī)表,如下所示:
DataFrame 結(jié)構(gòu)類似于 Execl 的表格型,表格中列標(biāo)簽的含義如下所示:
Regd.No:表示登記的序列號(hào)Name:學(xué)生姓名Marks:學(xué)生分?jǐn)?shù)
同 Series 一樣,DataFrame 自帶行標(biāo)簽索引,默認(rèn)為“隱式索引”即從 0 開始依次遞增,行標(biāo)簽與 DataFrame 中的數(shù)據(jù)項(xiàng)一一對(duì)應(yīng)。上述表格的行標(biāo)簽從 0 到 5,共記錄了 5 條數(shù)據(jù)(圖中將行標(biāo)簽省略)。當(dāng)然你也可以用“顯式索引”的方式來設(shè)置行標(biāo)簽。
下面對(duì) DataFrame 數(shù)據(jù)結(jié)構(gòu)的特點(diǎn)做簡(jiǎn)單地總結(jié),如下所示:
DataFrame 每一列的標(biāo)簽值允許使用不同的數(shù)據(jù)類型;DataFrame 是表格型的數(shù)據(jù)結(jié)構(gòu),具有行和列;DataFrame 中的每個(gè)數(shù)據(jù)值都可以被修改。DataFrame 結(jié)構(gòu)的行數(shù)、列數(shù)允許增加或者刪除;DataFrame 有兩個(gè)方向的標(biāo)簽軸,分別是行標(biāo)簽和列標(biāo)簽;DataFrame 可以對(duì)行和列執(zhí)行算術(shù)運(yùn)算。
創(chuàng)建DataFrame對(duì)象
創(chuàng)建 DataFrame 對(duì)象的語(yǔ)法格式如下:
import pandas as pd pd.DataFrame( data, index, columns, dtype, copy)
參數(shù)說明:
參數(shù)名稱說明data輸入的數(shù)據(jù),可以是 ndarray,series,list,dict,標(biāo)量以及一個(gè) DataFrame。index行標(biāo)簽,如果沒有傳遞 index 值,則默認(rèn)行標(biāo)簽是 np.arange(n),n 代表 data 的元素個(gè)數(shù)。columns列標(biāo)簽,如果沒有傳遞 columns 值,則默認(rèn)列標(biāo)簽是 np.arange(n)。dtypedtype表示每一列的數(shù)據(jù)類型。copy默認(rèn)為 False,表示復(fù)制數(shù)據(jù) data。
Pandas 提供了多種創(chuàng)建 DataFrame 對(duì)象的方式,主要包含以下五種,分別進(jìn)行介紹。
1) 創(chuàng)建空的DataFrame對(duì)象
使用下列方式創(chuàng)建一個(gè)空的 DataFrame,這是 DataFrame 最基本的創(chuàng)建方法。
import pandas as pd df = pd.DataFrame() print(df)
輸出結(jié)果如下:
Empty DataFrame Columns: [] Index: []
2) 列表創(chuàng)建DataFame對(duì)象
可以使用單一列表或嵌套列表來創(chuàng)建一個(gè) DataFrame。
示例 1,單一列表創(chuàng)建 DataFrame:
import pandas as pd data = [1,2,3,4,5] df = pd.DataFrame(data) print(df)
輸出如下:
0 0 1 1 2 2 3 3 4 4 5
示例 2,使用嵌套列表創(chuàng)建 DataFrame 對(duì)象:
import pandas as pd data = [[‘Alex’,10],[‘Bob’,12],[‘Clarke’,13]] df = pd.DataFrame(data,columns=[‘Name’,‘Age’]) print(df)
輸出結(jié)果:
Name Age 0 Alex 10 1 Bob 12 2 Clarke 13
示例 3,指定數(shù)值元素的數(shù)據(jù)類型為 float:
import pandas as pd data = [[‘Alex’,10],[‘Bob’,12],[‘Clarke’,13]] df = pd.DataFrame(data,columns=[‘Name’,‘Age’],dtype=float) print(df)
輸出結(jié)果:
Name Age 0 Alex 10.0 1 Bob 12.0 2 Clarke 13.0
3) 字典嵌套列表創(chuàng)建
data 字典中,鍵對(duì)應(yīng)的值的元素長(zhǎng)度必須相同(也就是列表長(zhǎng)度相同)。如果傳遞了索引,那么索引的長(zhǎng)度應(yīng)該等于數(shù)組的長(zhǎng)度;如果沒有傳遞索引,那么默認(rèn)情況下,索引將是 range(n),其中 n 代表數(shù)組長(zhǎng)度。
示例 4:
import pandas as pd data = {‘Name’:[‘Tom’, ‘Jack’, ‘Steve’, ‘Ricky’],‘Age’:[28,34,29,42]} df = pd.DataFrame(data) print(df)
輸出結(jié)果:
Age Name 0 28 Tom 1 34 Jack 2 29 Steve 3 42 Ricky
注意:這里使用了默認(rèn)行標(biāo)簽,也就是 range(n)。它生成了 0,1,2,3,并分別對(duì)應(yīng)了列表中的每個(gè)元素值。
示例 5,現(xiàn)在給上述示例 4 添加自定義的行標(biāo)簽:
import pandas as pd data = {‘Name’:[‘Tom’, ‘Jack’, ‘Steve’, ‘Ricky’],‘Age’:[28,34,29,42]} df = pd.DataFrame(data, index=[‘rank1’,‘rank2’,‘rank3’,‘rank4’]) print(df)
輸出結(jié)果如下:
Age Name rank1 28 Tom rank2 34 Jack rank3 29 Steve rank4 42 Ricky
注意:index 參數(shù)為每行分配了一個(gè)索引。
4) 列表嵌套字典創(chuàng)建DataFrame對(duì)象
列表嵌套字典可以作為輸入數(shù)據(jù)傳遞給 DataFrame 構(gòu)造函數(shù)。默認(rèn)情況下,字典的鍵被用作列名。
示例 6 如下:
import pandas as pd data = [{‘a(chǎn)’: 1, ‘b’: 2},{‘a(chǎn)’: 5, ‘b’: 10, ‘c’: 20}] df = pd.DataFrame(data) print(df)
輸出結(jié)果:
a b c 0 1 2 NaN 1 5 10 20.0
注意:如果其中某個(gè)元素值缺失,也就是字典的 key 無法找到對(duì)應(yīng)的 value,將使用 NaN 代替。
示例 7,給上述示例 6 添加行標(biāo)簽索引:
import pandas as pd data = [{‘a(chǎn)’: 1, ‘b’: 2},{‘a(chǎn)’: 5, ‘b’: 10, ‘c’: 20}] df = pd.DataFrame(data, index=[‘first’, ‘second’]) print(df)
輸出結(jié)果:
a b c first 1 2 NaN second 5 10 20.0
示例 8,如何使用字典嵌套列表以及行、列索引表創(chuàng)建一個(gè) DataFrame 對(duì)象。
import pandas as pd data = [{‘a(chǎn)’: 1, ‘b’: 2},{‘a(chǎn)’: 5, ‘b’: 10, ‘c’: 20}] df1 = pd.DataFrame(data, index=[‘first’, ‘second’], columns=[‘a(chǎn)’, ‘b’]) df2 = pd.DataFrame(data, index=[‘first’, ‘second’], columns=[‘a(chǎn)’, ‘b1’]) print(df1) print(df2)
輸出結(jié)果:
#df2輸出 a b first 1 2 second 5 10
#df1輸出 a b1 first 1 NaN second 5 NaN
注意:因?yàn)?b1 在字典鍵中不存在,所以對(duì)應(yīng)值為 NaN。
5) Series創(chuàng)建DataFrame對(duì)象
您也可以傳遞一個(gè)字典形式的 Series,從而創(chuàng)建一個(gè) DataFrame 對(duì)象,其輸出結(jié)果的行索引是所有 index 的合集。 示例如下:
import pandas as pd d = {‘one’ : pd.Series([1, 2, 3], index=[‘a(chǎn)’, ‘b’, ‘c’]), ‘two’ : pd.Series([1, 2, 3, 4], index=[‘a(chǎn)’, ‘b’, ‘c’, ‘d’])} df = pd.DataFrame(d) print(df)
輸出結(jié)果如下:
one two a 1.0 1 b 2.0 2 c 3.0 3 d NaN 4
注意:對(duì)于 one 列而言,此處雖然顯示了行索引 ‘d’,但由于沒有與其對(duì)應(yīng)的值,所以它的值為 NaN。
列索引操作DataFrame
DataFrame 可以使用列索(columns index)引來完成數(shù)據(jù)的選取、添加和刪除操作。下面依次對(duì)這些操作進(jìn)行介紹。
1) 列索引選取數(shù)據(jù)列
您可以使用列索引,輕松實(shí)現(xiàn)數(shù)據(jù)選取,示例如下:
import pandas as pd d = {‘one’ : pd.Series([1, 2, 3], index=[‘a(chǎn)’, ‘b’, ‘c’]), ‘two’ : pd.Series([1, 2, 3, 4], index=[‘a(chǎn)’, ‘b’, ‘c’, ‘d’])} df = pd.DataFrame(d) print(df [‘one’])
輸出結(jié)果:
a 1.0 b 2.0 c 3.0 d NaN Name: one, dtype: float64
2) 列索引添加數(shù)據(jù)列
使用 columns 列索引表標(biāo)簽可以實(shí)現(xiàn)添加新的數(shù)據(jù)列,示例如下:
import pandas as pd d = {‘one’ : pd.Series([1, 2, 3], index=[‘a(chǎn)’, ‘b’, ‘c’]), ‘two’ : pd.Series([1, 2, 3, 4], index=[‘a(chǎn)’, ‘b’, ‘c’, ‘d’])} df = pd.DataFrame(d) #使用df[‘列’]=值,插入新的數(shù)據(jù)列 df[‘three’]=pd.Series([10,20,30],index=[‘a(chǎn)’,‘b’,‘c’]) print(df) #將已經(jīng)存在的數(shù)據(jù)列做相加運(yùn)算 df[‘four’]=df[‘one’]+df[‘three’] print(df)
輸出結(jié)果:
使用列索引創(chuàng)建新數(shù)據(jù)列: one two three a 1.0 1 10.0 b 2.0 2 20.0 c 3.0 3 30.0 d NaN 4 NaN
已存在的數(shù)據(jù)列做算術(shù)運(yùn)算: one two three four a 1.0 1 10.0 11.0 b 2.0 2 20.0 22.0 c 3.0 3 30.0 33.0 d NaN 4 NaN NaN
上述示例,我們初次使用了 DataFrame 的算術(shù)運(yùn)算,這和 NumPy 非常相似。除了使用df[]=value的方式外,您還可以使用 insert() 方法插入新的列,示例如下:
import pandas as pd info=[[‘Jack’,18],[‘Helen’,19],[‘John’,17]] df=pd.DataFrame(info,columns=[‘name’,‘a(chǎn)ge’]) print(df) #注意是column參數(shù) #數(shù)值1代表插入到columns列表的索引位置 df.insert(1,column=‘score’,value=[91,90,75]) print(df)
輸出結(jié)果:
添加前: name age 0 Jack 18 1 Helen 19 2 John 17
添加后: name score age 0 Jack 91 18 1 Helen 90 19 2 John 75 17
3) 列索引刪除數(shù)據(jù)列
通過 del 和 pop() 都能夠刪除 DataFrame 中的數(shù)據(jù)列。示例如下:
import pandas as pd d = {‘one’ : pd.Series([1, 2, 3], index=[‘a(chǎn)’, ‘b’, ‘c’]), ‘two’ : pd.Series([1, 2, 3, 4], index=[‘a(chǎn)’, ‘b’, ‘c’, ‘d’]), ‘three’ : pd.Series([10,20,30], index=[‘a(chǎn)’,‘b’,‘c’])} df = pd.DataFrame(d) print (“Our dataframe is:”) print(df) #使用del刪除 del df[‘one’] print(df) #使用pop方法刪除 df.pop(‘two’) print (df)
輸出結(jié)果:
原DataFrame: one three two a 1.0 10.0 1 b 2.0 20.0 2 c 3.0 30.0 3 d NaN NaN 4
使用del刪除 first: three two a 10.0 1 b 20.0 2 c 30.0 3 d NaN 4
使用 pop()刪除: three a 10.0 b 20.0 c 30.0 d NaN
行索引操作DataFrame
理解了上述的列索引操作后,行索引操作就變的簡(jiǎn)單。下面看一下,如何使用行索引來選取 DataFrame 中的數(shù)據(jù)。
1) 標(biāo)簽索引選取
可以將行標(biāo)簽傳遞給 loc 函數(shù),來選取數(shù)據(jù)。示例如下:
import pandas as pd d = {‘one’ : pd.Series([1, 2, 3], index=[‘a(chǎn)’, ‘b’, ‘c’]), ‘two’ : pd.Series([1, 2, 3, 4], index=[‘a(chǎn)’, ‘b’, ‘c’, ‘d’])} df = pd.DataFrame(d) print(df.loc[‘b’])
輸出結(jié)果:
one 2.0two 2.0Name: b, dtype: float64
注意:loc 允許接兩個(gè)參數(shù)分別是行和列,參數(shù)之間需要使用“逗號(hào)”隔開,但該函數(shù)只能接收標(biāo)簽索引。
2) 整數(shù)索引選取
通過將數(shù)據(jù)行所在的索引位置傳遞給 iloc 函數(shù),也可以實(shí)現(xiàn)數(shù)據(jù)行選取。示例如下:
import pandas as pd d = {‘one’ : pd.Series([1, 2, 3], index=[‘a(chǎn)’, ‘b’, ‘c’]), ‘two’ : pd.Series([1, 2, 3, 4], index=[‘a(chǎn)’, ‘b’, ‘c’, ‘d’])} df = pd.DataFrame(d) print (df.iloc[2])
輸出結(jié)果:
one 3.0 two 3.0 Name: c, dtype: float64
注意:iloc 允許接受兩個(gè)參數(shù)分別是行和列,參數(shù)之間使用“逗號(hào)”隔開,但該函數(shù)只能接收整數(shù)索引。
3) 切片操作多行選取
您也可以使用切片的方式同時(shí)選取多行。示例如下:
import pandas as pd d = {‘one’ : pd.Series([1, 2, 3], index=[‘a(chǎn)’, ‘b’, ‘c’]), ‘two’ : pd.Series([1, 2, 3, 4], index=[‘a(chǎn)’, ‘b’, ‘c’, ‘d’])} df = pd.DataFrame(d) #左閉右開 print(df[2:4])
輸出結(jié)果:
one two c 3.0 3 d NaN 4
4) 添加數(shù)據(jù)行
使用 append() 函數(shù),可以將新的數(shù)據(jù)行添加到 DataFrame 中,該函數(shù)會(huì)在行末追加數(shù)據(jù)行。示例如下:
import pandas as pd df = pd.DataFrame([[1, 2], [3, 4]], columns = [‘a(chǎn)’,‘b’]) df2 = pd.DataFrame([[5, 6], [7, 8]], columns = [‘a(chǎn)’,‘b’]) #在行末追加新數(shù)據(jù)行 df = df.append(df2) print(df)
輸出結(jié)果:
a b 0 1 2 1 3 4 0 5 6 1 7 8
5) 刪除數(shù)據(jù)行
您可以使用行索引標(biāo)簽,從 DataFrame 中刪除某一行數(shù)據(jù)。如果索引標(biāo)簽存在重復(fù),那么它們將被一起刪除。示例如下:
import pandas as pd df = pd.DataFrame([[1, 2], [3, 4]], columns = [‘a(chǎn)’,‘b’]) df2 = pd.DataFrame([[5, 6], [7, 8]], columns = [‘a(chǎn)’,‘b’]) df = df.append(df2) print(df) #注意此處調(diào)用了drop()方法 df = df.drop(0) print (df)
輸出結(jié)果:
執(zhí)行drop(0)前: a b 0 1 2 1 3 4 0 5 6 1 7 8
執(zhí)行drop(0)后: a b 1 3 4 1 7 8
在上述的示例中,默認(rèn)使用 range(2) 生成了行索引,并通過 drop(0) 同時(shí)刪除了兩行數(shù)據(jù)。
常用屬性和方法匯總
DataFrame 的屬性和方法,與 Series 相差無幾,如下所示:
名稱屬性&方法描述T行和列轉(zhuǎn)置。axes返回一個(gè)僅以行軸標(biāo)簽和列軸標(biāo)簽為成員的列表。dtypes返回每列數(shù)據(jù)的數(shù)據(jù)類型。emptyDataFrame中沒有數(shù)據(jù)或者任意坐標(biāo)軸的長(zhǎng)度為0,則返回True。ndim軸的數(shù)量,也指數(shù)組的維數(shù)。shape返回一個(gè)元組,表示了 DataFrame 維度。sizeDataFrame中的元素?cái)?shù)量。values使用 numpy 數(shù)組表示 DataFrame 中的元素值。head()返回前 n 行數(shù)據(jù)。tail()返回后 n 行數(shù)據(jù)。shift()將行或列移動(dòng)指定的步幅長(zhǎng)度
下面對(duì) DataFrame 常用屬性進(jìn)行演示,首先我們創(chuàng)建一個(gè) DataFrame 對(duì)象,示例如下:
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語(yǔ)言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學(xué)苑’,‘Bing搜索’]), ‘years’:pd.Series([5,6,15,28,3,19,23]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #構(gòu)建DataFrame df = pd.DataFrame(d) #輸出series print(df)
輸出結(jié)果:
輸出 series 數(shù)據(jù): Name years Rating 0 c語(yǔ)言中文網(wǎng) 5 4.23 1 編程幫 6 3.24 2 百度 15 3.98 3 360搜索 28 2.56 4 谷歌 3 3.20 5 微學(xué)苑 19 4.60 6 Bing搜索 23 3.80
1) T(Transpose)轉(zhuǎn)置
返回 DataFrame 的轉(zhuǎn)置,也就是把行和列進(jìn)行交換。
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語(yǔ)言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學(xué)苑’,‘Bing搜索’]), ‘years’:pd.Series([5,6,15,28,3,19,23]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #構(gòu)建DataFrame df = pd.DataFrame(d) #輸出DataFrame的轉(zhuǎn)置 print(df.T)
輸出結(jié)果:
Our data series is: 0 1 2 3 4 5 6 Name c語(yǔ)言中文網(wǎng) 編程幫 百度 360搜索 谷歌 微學(xué)苑 Bing搜索 years 5 6 15 28 3 19 23 Rating 4.23 3.24 3.98 2.56 3.2 4.6 3.8
2) axes
返回一個(gè)行標(biāo)簽、列標(biāo)簽組成的列表。
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語(yǔ)言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學(xué)苑’,‘Bing搜索’]), ‘years’:pd.Series([5,6,15,28,3,19,23]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #構(gòu)建DataFrame df = pd.DataFrame(d) #輸出行、列標(biāo)簽 print(df.axes)
輸出結(jié)果:
[RangeIndex(start=0, stop=7, step=1), Index([‘Name’, ‘years’, ‘Rating’], dtype=‘object’)]
3) dtypes
返回每一列的數(shù)據(jù)類型。示例如下:
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語(yǔ)言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學(xué)苑’,‘Bing搜索’]), ‘years’:pd.Series([5,6,15,28,3,19,23]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #構(gòu)建DataFrame df = pd.DataFrame(d) #輸出行、列標(biāo)簽 print(df.dtypes)
輸出結(jié)果:
Name object years int64 Rating float64 dtype: object
4) empty
返回一個(gè)布爾值,判斷輸出的數(shù)據(jù)對(duì)象是否為空,若為 True 表示對(duì)象為空。
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語(yǔ)言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學(xué)苑’,‘Bing搜索’]), ‘years’:pd.Series([5,6,15,28,3,19,23]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #構(gòu)建DataFrame df = pd.DataFrame(d) #判斷輸入數(shù)據(jù)是否為空 print(df.empty)
輸出結(jié)果:
判斷輸入對(duì)象是否為空: False
5) ndim
返回?cái)?shù)據(jù)對(duì)象的維數(shù)。DataFrame 是一個(gè)二維數(shù)據(jù)結(jié)構(gòu)。
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語(yǔ)言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學(xué)苑’,‘Bing搜索’]), ‘years’:pd.Series([5,6,15,28,3,19,23]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #構(gòu)建DataFrame df = pd.DataFrame(d) #DataFrame的維度 print(df.ndim)
輸出結(jié)果:
2
6) shape
返回一個(gè)代表 DataFrame 維度的元組。返回值元組 (a,b),其中 a 表示行數(shù),b 表示列數(shù)。
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語(yǔ)言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學(xué)苑’,‘Bing搜索’]), ‘years’:pd.Series([5,6,15,28,3,19,23]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #構(gòu)建DataFrame df = pd.DataFrame(d) #DataFrame的形狀 print(df.shape) 輸出結(jié)果:
(7, 3)
7) size
返回 DataFrame 中的元素?cái)?shù)量。示例如下:
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語(yǔ)言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學(xué)苑’,‘Bing搜索’]), ‘years’:pd.Series([5,6,15,28,3,19,23]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #構(gòu)建DataFrame df = pd.DataFrame(d) #DataFrame的中元素個(gè)數(shù) print(df.size)
輸出結(jié)果:
21
8) values
以 ndarray 數(shù)組的形式返回 DataFrame 中的數(shù)據(jù)。
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語(yǔ)言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學(xué)苑’,‘Bing搜索’]), ‘years’:pd.Series([5,6,15,28,3,19,23]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #構(gòu)建DataFrame df = pd.DataFrame(d) #DataFrame的數(shù)據(jù) print(df.values)
輸出結(jié)果:
[[‘c語(yǔ)言中文網(wǎng)’ 5 4.23] [‘編程幫’ 6 3.24] [‘百度’ 15 3.98] [‘360搜索’ 28 2.56] [‘谷歌’ 3 3.2] [‘微學(xué)苑’ 19 4.6] [‘Bing搜索’ 23 3.8]]
9) head()&tail()查看數(shù)據(jù)
如果想要查看 DataFrame 的一部分?jǐn)?shù)據(jù),可以使用 head() 或者 tail() 方法。其中 head() 返回前 n 行數(shù)據(jù),默認(rèn)顯示前 5 行數(shù)據(jù)。示例如下:
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語(yǔ)言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學(xué)苑’,‘Bing搜索’]), ‘years’:pd.Series([5,6,15,28,3,19,23]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #構(gòu)建DataFrame df = pd.DataFrame(d) #獲取前3行數(shù)據(jù) print(df.head(3))
輸出結(jié)果:
Name years Rating 0 c語(yǔ)言中文網(wǎng) 5 4.23 1 編程幫 6 3.24 2 百度 15 3.98
tail() 返回后 n 行數(shù)據(jù),示例如下:
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語(yǔ)言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學(xué)苑’,‘Bing搜索’]), ‘years’:pd.Series([5,6,15,28,3,19,23]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #構(gòu)建DataFrame df = pd.DataFrame(d) #獲取后2行數(shù)據(jù) print(df.tail(2))
輸出結(jié)果:
Name years Rating 5 微學(xué)苑 19 4.6 6 Bing搜索 23 3.8
10) shift()移動(dòng)行或列
如果您想要移動(dòng) DataFrame 中的某一行/列,可以使用 shift() 函數(shù)實(shí)現(xiàn)。它提供了一個(gè)periods參數(shù),該參數(shù)表示在特定的軸上移動(dòng)指定的步幅。
shif() 函數(shù)的語(yǔ)法格式如下:
DataFrame.shift(periods=1, freq=None, axis=0)
參數(shù)說明如下:
參數(shù)名稱說明peroids類型為int,表示移動(dòng)的幅度,可以是正數(shù),也可以是負(fù)數(shù),默認(rèn)值為1。freq日期偏移量,默認(rèn)值為None,適用于時(shí)間序。取值為符合時(shí)間規(guī)則的字符串。axis如果是 0 或者 “index” 表示上下移動(dòng),如果是 1 或者 “columns” 則會(huì)左右移動(dòng)。fill_value該參數(shù)用來填充缺失值。
該函數(shù)的返回值是移動(dòng)后的 DataFrame 副本。下面看一組簡(jiǎn)單的實(shí)例:
import pandas as pd info= pd.DataFrame({‘a(chǎn)_data’: [40, 28, 39, 32, 18], ‘b_data’: [20, 37, 41, 35, 45], ‘c_data’: [22, 17, 11, 25, 15]}) #移動(dòng)幅度為3 info.shift(periods=3)
輸出結(jié)果:
a_data b_data c_data 0 NaN NaN NaN 1 NaN NaN NaN 2 NaN NaN NaN 3 40.0 20.0 22.0 4 28.0 37.0 17.0
下面使用 fill_value 參數(shù)填充 DataFrame 中的缺失值,如下所示:
import pandas as pd info= pd.DataFrame({‘a(chǎn)_data’: [40, 28, 39, 32, 18], ‘b_data’: [20, 37, 41, 35, 45], ‘c_data’: [22, 17, 11, 25, 15]}) #移動(dòng)幅度為3 print(info.shift(periods=3)) #將缺失值和原數(shù)值替換為52 info.shift(periods=3,axis=1,fill_value= 52)
輸出結(jié)果:
原輸出結(jié)果: a_data b_data c_data 0 NaN NaN NaN 1 NaN NaN NaN 2 NaN NaN NaN 3 40.0 20.0 22.0 4 28.0 37.0 17.0
替換后輸出: a_data b_data c_data 0 52 52 52 1 52 52 52 2 52 52 52 3 52 52 52 4 52 52 52
注意:fill_value 參數(shù)不僅可以填充缺失值,還也可以對(duì)原數(shù)據(jù)進(jìn)行替換。
Pandas Panel三維數(shù)據(jù)結(jié)構(gòu)
Panel 結(jié)構(gòu)也稱“面板結(jié)構(gòu)”,它源自于 Panel Data 一詞,翻譯為“面板數(shù)據(jù)”。如果您使用的是 Pandas 0.25 以前的版本,那么您需要掌握本節(jié)內(nèi)容,否則,作為了解內(nèi)容即可。
自 Pandas 0.25 版本后, Panel 結(jié)構(gòu)已經(jīng)被廢棄。
Panel 是一個(gè)用來承載數(shù)據(jù)的三維數(shù)據(jù)結(jié)構(gòu),它有三個(gè)軸,分別是 items(0 軸),major_axis(1 軸),而 minor_axis(2 軸)。這三個(gè)軸為描述、操作 Panel 提供了支持,其作用介紹如下:
items:axis =0,Panel 中的每個(gè) items 都對(duì)應(yīng)一個(gè) DataFrame。major_axis:axis=1,用來描述每個(gè) DataFrame 的行索引。minor_axis:axis=2,用來描述每個(gè) DataFrame 的列索引。
pandas.Panel()
您可以使用下列構(gòu)造函數(shù)創(chuàng)建一個(gè) Panel,如下所示:
pandas.Panel(data, items, major_axis, minor_axis, dtype, copy)
參數(shù)說明如下:
參數(shù)名稱描述說明data輸入數(shù)據(jù),可以是 ndarray,Series,列表,字典,或者 DataFrame。itemsaxis=0major_axisaxis=1minor_axisaxis=2dtype每一列的數(shù)據(jù)類型。copy默認(rèn)為 False,表示是否復(fù)制數(shù)據(jù)。
創(chuàng)建Panel 對(duì)象
下面介紹創(chuàng)建 Panel 對(duì)象的兩種方式:一種是使用 nadarry 數(shù)組創(chuàng)建,另一種使用 DataFrame 對(duì)象創(chuàng)建。首先,我們學(xué)習(xí)如何創(chuàng)建一個(gè)空的 Panel 對(duì)象。
1) 創(chuàng)建一個(gè)空Panel
使用 Panel 的構(gòu)造函數(shù)創(chuàng)建,如下所示:
import pandas as pd p = pd.Panel() print§
輸出結(jié)果:
2) ndarray三維數(shù)組創(chuàng)建
import pandas as pd import numpy as np #返回均勻分布的隨機(jī)樣本值位于[0,1)之間 data = np.random.rand(2,4,5) p = pd.Panel(data) print §
輸出結(jié)果:
請(qǐng)注意與上述示例的空 Panel 進(jìn)行對(duì)比。
3) DataFrame創(chuàng)建
下面使用 DataFrame 創(chuàng)建一個(gè) Panel,示例如下:
import pandas as pd import numpy as np data = {‘Item1’ : pd.DataFrame(np.random.randn(4, 3)), ‘Item2’ : pd.DataFrame(np.random.randn(4, 2))} p = pd.Panel(data) print§
輸出結(jié)果:
Dimensions: 2 (items) x 4 (major_axis) x 3 (minor_axis) Items axis: Item1 to Item2 Major_axis axis: 0 to 3 Minor_axis axis: 0 to 2
Panel中選取數(shù)據(jù)
如果想要從 Panel 對(duì)象中選取數(shù)據(jù),可以使用 Panel 的三個(gè)軸來實(shí)現(xiàn),也就是items,major_axis,minor_axis。下面介紹其中一種,大家體驗(yàn)一下即可。
1) 使用 items選取數(shù)據(jù)
示例如下:
import pandas as pd import numpy as np data = {‘Item1’:pd.DataFrame(np.random.randn(4, 3)), ‘Item2’:pd.DataFrame(np.random.randn(4, 2))} p = pd.Panel(data) print(p[‘Item1’])
輸出結(jié)果:
0 1 2 0 0.488224 -0.128637 0.930817 1 0.417497 0.896681 0.576657 2 -2.775266 0.571668 0.290082 3 -0.400538 -0.144234 1.110535
上述示例中 data,包含了兩個(gè)數(shù)據(jù)項(xiàng),我們選擇了 item1,輸出結(jié)果是 4 行 3 列的 DataFrame,其行、列索引分別對(duì)應(yīng) major_axis 和 minor_axis。
Python Pandas描述性統(tǒng)計(jì)
描述統(tǒng)計(jì)學(xué)(descriptive statistics)是一門統(tǒng)計(jì)學(xué)領(lǐng)域的學(xué)科,主要研究如何取得反映客觀現(xiàn)象的數(shù)據(jù),并以圖表形式對(duì)所搜集的數(shù)據(jù)進(jìn)行處理和顯示,最終對(duì)數(shù)據(jù)的規(guī)律、特征做出綜合性的描述分析。Pandas 庫(kù)正是對(duì)描述統(tǒng)計(jì)學(xué)知識(shí)完美應(yīng)用的體現(xiàn),可以說如果沒有“描述統(tǒng)計(jì)學(xué)”作為理論基奠,那么 Pandas 是否存在猶未可知。下列表格對(duì) Pandas 常用的統(tǒng)計(jì)學(xué)函數(shù)做了簡(jiǎn)單的總結(jié):
函數(shù)名稱描述說明count()統(tǒng)計(jì)某個(gè)非空值的數(shù)量。sum()求和mean()求均值median()求中位數(shù)mode()求眾數(shù)std()求標(biāo)準(zhǔn)差min()求最小值max()求最大值abs()求絕對(duì)值prod()求所有數(shù)值的乘積。cumsum()計(jì)算累計(jì)和,axis=0,按照行累加;axis=1,按照列累加。cumprod()計(jì)算累計(jì)積,axis=0,按照行累積;axis=1,按照列累積。corr()計(jì)算數(shù)列或變量之間的相關(guān)系數(shù),取值-1到1,值越大表示關(guān)聯(lián)性越強(qiáng)。
從描述統(tǒng)計(jì)學(xué)角度出發(fā),我們可以對(duì) DataFrame 結(jié)構(gòu)執(zhí)行聚合計(jì)算等其他操作,比如 sum() 求和、mean()求均值等方法。
在 DataFrame 中,使用聚合類方法時(shí)需要指定軸(axis)參數(shù)。下面介紹兩種傳參方式:
對(duì)行操作,默認(rèn)使用 axis=0 或者使用 “index”;對(duì)列操作,默認(rèn)使用 axis=1 或者使用 “columns”。
圖1:axis軸示意圖
從圖 1 可以看出,axis=0 表示按垂直方向進(jìn)行計(jì)算,而 axis=1 則表示按水平方向。下面讓我們創(chuàng)建一個(gè) DataFrame,使用它對(duì)本節(jié)的內(nèi)容進(jìn)行演示。
創(chuàng)建一個(gè) DataFrame 結(jié)構(gòu),如下所示:
import pandas as pd import numpy as np #創(chuàng)建字典型series結(jié)構(gòu) d = {‘Name’:pd.Series([‘小明’,‘小亮’,‘小紅’,‘小華’,‘老趙’,‘小曹’,‘小陳’, ‘老李’,‘老王’,‘小馮’,‘小何’,‘老張’]), ‘Age’:pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) } df = pd.DataFrame(d) print(df)
輸出結(jié)果:
Name Age Rating 0 小明 25 4.23 1 小亮 26 3.24 2 小紅 25 3.98 3 小華 23 2.56 4 老趙 30 3.20 5 小曹 29 4.60 6 小陳 23 3.80 7 老李 34 3.78 8 老王 40 2.98 9 小馮 30 4.80 10 小何 51 4.10 11 老張 46 3.65
sum()求和
在默認(rèn)情況下,返回 axis=0 的所有值的和。示例如下:
import pandas as pd import numpy as np #創(chuàng)建字典型series結(jié)構(gòu) d = {‘Name’:pd.Series([‘小明’,‘小亮’,‘小紅’,‘小華’,‘老趙’,‘小曹’,‘小陳’, ‘老李’,‘老王’,‘小馮’,‘小何’,‘老張’]), ‘Age’:pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) } df = pd.DataFrame(d) #默認(rèn)axis=0或者使用sum(“index”) print(df.sum())
輸出結(jié)果:
Name 小明小亮小紅小華老趙小曹小陳老李老王小馮小何老張 Age 382 Rating 44.92 dtype: object
注意:sum() 和 cumsum() 函數(shù)可以同時(shí)處理數(shù)字和字符串?dāng)?shù)據(jù)。雖然字符聚合通常不被使用,但使用這兩個(gè)函數(shù)并不會(huì)拋出異常;而對(duì)于 abs()、cumprod() 函數(shù)則會(huì)拋出異常,因?yàn)樗鼈儫o法操作字符串?dāng)?shù)據(jù)。
下面再看一下 axis=1 的情況,如下所示:
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘小明’,‘小亮’,‘小紅’,‘小華’,‘老趙’,‘小曹’,‘小陳’, ‘老李’,‘老王’,‘小馮’,‘小何’,‘老張’]), ‘Age’:pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) } df = pd.DataFrame(d) #也可使用sum(“columns”)或sum(1) print(df.sum(axis=1))
輸出結(jié)果:
0 29.23 1 29.24 2 28.98 3 25.56 4 33.20 5 33.60 6 26.80 7 37.78 8 42.98 9 34.80 10 55.10 11 49.65 dtype: float64
mean()求均值
示例如下:
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘小明’,‘小亮’,‘小紅’,‘小華’,‘老趙’,‘小曹’,‘小陳’, ‘老李’,‘老王’,‘小馮’,‘小何’,‘老張’]), ‘Age’:pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) } df = pd.DataFrame(d) print(df.mean())
輸出結(jié)果:
Age 31.833333 Rating 3.743333 dtype: float64
std()求標(biāo)準(zhǔn)差
返回?cái)?shù)值列的標(biāo)準(zhǔn)差,示例如下:
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘小明’,‘小亮’,‘小紅’,‘小華’,‘老趙’,‘小曹’,‘小陳’, ‘老李’,‘老王’,‘小馮’,‘小何’,‘老張’]), ‘Age’:pd.Series([25,26,25,23,59,19,23,44,40,30,51,54]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) } df = pd.DataFrame(d) print(df.std())
輸出結(jié)果:
Age 13.976983 Rating 0.661628 dtype: float64
標(biāo)準(zhǔn)差是方差的算術(shù)平方根,它能反映一個(gè)數(shù)據(jù)集的離散程度。注意,平均數(shù)相同的兩組數(shù)據(jù),標(biāo)準(zhǔn)差未必相同。
數(shù)據(jù)匯總描述
describe() 函數(shù)顯示與 DataFrame 數(shù)據(jù)列相關(guān)的統(tǒng)計(jì)信息摘要。示例如下:
自我介紹一下,小編13年上海交大畢業(yè),曾經(jīng)在小公司待過,也去過華為、OPPO等大廠,18年進(jìn)入阿里一直到現(xiàn)在。
深知大多數(shù)Go語(yǔ)言工程師,想要提升技能,往往是自己摸索成長(zhǎng)或者是報(bào)班學(xué)習(xí),但對(duì)于培訓(xùn)機(jī)構(gòu)動(dòng)則幾千的學(xué)費(fèi),著實(shí)壓力不小。自己不成體系的自學(xué)效果低效又漫長(zhǎng),而且極易碰到天花板技術(shù)停滯不前!
因此收集整理了一份《2024年Go語(yǔ)言全套學(xué)習(xí)資料》,初衷也很簡(jiǎn)單,就是希望能夠幫助到想自學(xué)提升又不知道該從何學(xué)起的朋友,同時(shí)減輕大家的負(fù)擔(dān)。
既有適合小白學(xué)習(xí)的零基礎(chǔ)資料,也有適合3年以上經(jīng)驗(yàn)的小伙伴深入學(xué)習(xí)提升的進(jìn)階課程,基本涵蓋了95%以上Golang知識(shí)點(diǎn),真正體系化!
由于文件比較大,這里只是將部分目錄大綱截圖出來,每個(gè)節(jié)點(diǎn)里面都包含大廠面經(jīng)、學(xué)習(xí)筆記、源碼講義、實(shí)戰(zhàn)項(xiàng)目、講解視頻,并且后續(xù)會(huì)持續(xù)更新
如果你覺得這些內(nèi)容對(duì)你有幫助,可以添加V獲?。簐ip1024b (備注Go)
一個(gè)人可以走的很快,但一群人才能走的更遠(yuǎn)。不論你是正從事IT行業(yè)的老鳥或是對(duì)IT行業(yè)感興趣的新人,都?xì)g迎掃碼加入我們的的圈子(技術(shù)交流、學(xué)習(xí)資源、職場(chǎng)吐槽、大廠內(nèi)推、面試輔導(dǎo)),讓我們一起學(xué)習(xí)成長(zhǎng)!
s([25,26,25,23,30,29,23,34,40,30,51,46]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) } df = pd.DataFrame(d) #也可使用sum(“columns”)或sum(1) print(df.sum(axis=1))
輸出結(jié)果:
0 29.23 1 29.24 2 28.98 3 25.56 4 33.20 5 33.60 6 26.80 7 37.78 8 42.98 9 34.80 10 55.10 11 49.65 dtype: float64
mean()求均值
示例如下:
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘小明’,‘小亮’,‘小紅’,‘小華’,‘老趙’,‘小曹’,‘小陳’, ‘老李’,‘老王’,‘小馮’,‘小何’,‘老張’]), ‘Age’:pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) } df = pd.DataFrame(d) print(df.mean())
輸出結(jié)果:
Age 31.833333 Rating 3.743333 dtype: float64
std()求標(biāo)準(zhǔn)差
返回?cái)?shù)值列的標(biāo)準(zhǔn)差,示例如下:
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘小明’,‘小亮’,‘小紅’,‘小華’,‘老趙’,‘小曹’,‘小陳’, ‘老李’,‘老王’,‘小馮’,‘小何’,‘老張’]), ‘Age’:pd.Series([25,26,25,23,59,19,23,44,40,30,51,54]), ‘Rating’:pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65]) } df = pd.DataFrame(d) print(df.std())
輸出結(jié)果:
Age 13.976983 Rating 0.661628 dtype: float64
標(biāo)準(zhǔn)差是方差的算術(shù)平方根,它能反映一個(gè)數(shù)據(jù)集的離散程度。注意,平均數(shù)相同的兩組數(shù)據(jù),標(biāo)準(zhǔn)差未必相同。
數(shù)據(jù)匯總描述
describe() 函數(shù)顯示與 DataFrame 數(shù)據(jù)列相關(guān)的統(tǒng)計(jì)信息摘要。示例如下:
自我介紹一下,小編13年上海交大畢業(yè),曾經(jīng)在小公司待過,也去過華為、OPPO等大廠,18年進(jìn)入阿里一直到現(xiàn)在。
深知大多數(shù)Go語(yǔ)言工程師,想要提升技能,往往是自己摸索成長(zhǎng)或者是報(bào)班學(xué)習(xí),但對(duì)于培訓(xùn)機(jī)構(gòu)動(dòng)則幾千的學(xué)費(fèi),著實(shí)壓力不小。自己不成體系的自學(xué)效果低效又漫長(zhǎng),而且極易碰到天花板技術(shù)停滯不前!
因此收集整理了一份《2024年Go語(yǔ)言全套學(xué)習(xí)資料》,初衷也很簡(jiǎn)單,就是希望能夠幫助到想自學(xué)提升又不知道該從何學(xué)起的朋友,同時(shí)減輕大家的負(fù)擔(dān)。 [外鏈圖片轉(zhuǎn)存中…(img-w37ZLIlO-1712980024109)] [外鏈圖片轉(zhuǎn)存中…(img-e7zfOMmm-1712980024110)] [外鏈圖片轉(zhuǎn)存中…(img-l6znK22j-1712980024111)] [外鏈圖片轉(zhuǎn)存中…(img-kACfZbqU-1712980024111)] [外鏈圖片轉(zhuǎn)存中…(img-08sMwyvR-1712980024112)]
既有適合小白學(xué)習(xí)的零基礎(chǔ)資料,也有適合3年以上經(jīng)驗(yàn)的小伙伴深入學(xué)習(xí)提升的進(jìn)階課程,基本涵蓋了95%以上Golang知識(shí)點(diǎn),真正體系化!
由于文件比較大,這里只是將部分目錄大綱截圖出來,每個(gè)節(jié)點(diǎn)里面都包含大廠面經(jīng)、學(xué)習(xí)筆記、源碼講義、實(shí)戰(zhàn)項(xiàng)目、講解視頻,并且后續(xù)會(huì)持續(xù)更新
如果你覺得這些內(nèi)容對(duì)你有幫助,可以添加V獲取:vip1024b (備注Go) [外鏈圖片轉(zhuǎn)存中…(img-gP12JjfX-1712980024112)]
一個(gè)人可以走的很快,但一群人才能走的更遠(yuǎn)。不論你是正從事IT行業(yè)的老鳥或是對(duì)IT行業(yè)感興趣的新人,都?xì)g迎掃碼加入我們的的圈子(技術(shù)交流、學(xué)習(xí)資源、職場(chǎng)吐槽、大廠內(nèi)推、面試輔導(dǎo)),讓我們一起學(xué)習(xí)成長(zhǎng)!
柚子快報(bào)邀請(qǐng)碼778899分享:Pandas教程(非常詳細(xì))
好文鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。