柚子快報邀請碼778899分享:Pandas教程(非常詳細)
柚子快報邀請碼778899分享:Pandas教程(非常詳細)
print(s)
輸出結(jié)果:
100 a 101 b 102 c 103 d dtype: object
3) dict創(chuàng)建Series對象
您可以把 dict 作為輸入數(shù)據(jù)。如果沒有傳入索引時會按照字典的鍵來構(gòu)造索引;反之,當傳遞了索引時需要將索引標簽與字典中的值一一對應。
下面兩組示例分別對上述兩種情況做了演示。
示例1,沒有傳遞索引時:
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ù)傳遞索引時:
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
當傳遞的索引值無法找到與其對應的值時,使用 NaN(非數(shù)字)填充。
4) 標量創(chuàng)建Series對象
如果 data 是標量值,則必須提供索引,示例如下:
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
標量值按照 index 的數(shù)量進行重復,并與其一一對應。
訪問Series數(shù)據(jù)
上述講解了創(chuàng)建 Series 對象的多種方式,那么我們應該如何訪問 Series 序列中元素呢?分為兩種方式,一種是位置索引訪問;另一種是索引標簽訪問。
1) 位置索引訪問
這種訪問方式與 ndarray 和 list 相同,使用元素自身的下標進行訪問。我們知道數(shù)組的索引計數(shù)從 0 開始,這表示第一個元素存儲在第 0 個索引位置上,以此類推,就可以獲得 Series 序列中的每個元素。下面看一組簡單的示例:
import pandas as pd s = pd.Series([1,2,3,4,5],index = [‘a(chǎn)’,‘b’,‘c’,‘d’,‘e’]) print(s[0]) #位置下標 print(s[‘a(chǎn)’]) #標簽下標
輸出結(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
如果想要獲取最后三個元素,也可以使用下面的方式:
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) 索引標簽訪問
Series 類似于固定大小的 dict,把 index 中的索引標簽當做 key,而把 Series 序列中的元素值當做 value,然后通過 index 索引標簽來訪問或者修改元素值。
示例1,使用索標簽訪問單個元素值:
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,使用索引標簽訪問多個元素值
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 中不包含的標簽,則會觸發(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 對象的常用屬性。
名稱屬性axes以列表的形式返回所有行索引標簽。dtype返回對象的數(shù)據(jù)類型。empty返回一個空的 Series 對象。ndim返回輸入數(shù)據(jù)的維數(shù)。size返回輸入數(shù)據(jù)的元素數(shù)量。values以 ndarray 的形式返回 Series 對象。index返回一個RangeIndex對象,用來描述索引的取值范圍。
現(xiàn)在創(chuàng)建一個 Series 對象,并演示如何使用上述表格中的屬性。如下所示:
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
上述示例的行索引標簽是 [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
返回一個布爾值,用于判斷數(shù)據(jù)對象是否為空。示例如下:
import pandas as pd import numpy as np s = pd.Series(np.random.randn(5)) print(“是否為空對象?”) print (s.empty)
輸出結(jié)果:
是否為空對象? 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 對象的大小(長度)。
import pandas as pd import numpy as np s = pd.Series(np.random.randn(3)) print (s) #series的長度大小 print(s.size)
輸出結(jié)果:
0 -1.866261 1 -0.636726 2 0.586037 dtype: float64 3
6) values
以數(shù)組的形式返回 Series 對象中的數(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 的某一部分數(shù)據(jù),可以使用 head() 或者 tail() 方法。其中 head() 返回前 n 行數(shù)據(jù),默認顯示前 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ù),默認為后 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()檢測缺失值
isnull() 和 nonull() 用于檢測 Series 中的缺失值。所謂缺失值,顧名思義就是值不存在、丟失、缺少。
isnull():如果為值不存在或者缺失,則返回 True。notnull():如果值不存在或者缺失,則返回 False。
其實不難理解,在實際的數(shù)據(jù)分析任物中,數(shù)據(jù)的收集往往要經(jīng)歷一個繁瑣的過程。在這個過程中難免會因為一些不可抗力,或者人為因素導致數(shù)據(jù)丟失的現(xiàn)象。這時,我們可以使用相應的方法對缺失值進行處理,比如均值插值、數(shù)據(jù)補齊等方法。上述兩個方法就是幫助我們檢測是否存在缺失值。示例如下:
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 進行數(shù)據(jù)分析過程中最常用的結(jié)構(gòu)之一,可以這么說,掌握了 DataFrame 的用法,你就擁有了學習數(shù)據(jù)分析的基本能力。
認識DataFrame結(jié)構(gòu)
DataFrame 一個表格型的數(shù)據(jù)結(jié)構(gòu),既有行標簽(index),又有列標簽(columns),它也被稱異構(gòu)數(shù)據(jù)表,所謂異構(gòu),指的是表格中每列的數(shù)據(jù)類型可以不同,比如可以是字符串、整型或者浮點型等。其結(jié)構(gòu)圖示意圖,如下所示:
表格中展示了某個銷售團隊個人信息和績效評級(rating)的相關數(shù)據(jù)。數(shù)據(jù)以行和列形式來表示,其中每一列表示一個屬性,而每一行表示一個條目的信息。
下表展示了上述表格中每一列標簽所描述數(shù)據(jù)的數(shù)據(jù)類型,如下所示:
ColumnTypenameStringageintegergenderStringratingFloat
DataFrame 的每一行數(shù)據(jù)都可以看成一個 Series 結(jié)構(gòu),只不過,DataFrame 為這些行中每個數(shù)據(jù)值增加了一個列標簽。因此 DataFrame 其實是從 Series 的基礎上演變而來。在數(shù)據(jù)分析任務中 DataFrame 的應用非常廣泛,因為它描述數(shù)據(jù)的更為清晰、直觀。
通過示例對 DataFrame 結(jié)構(gòu)做進一步講解。 下面展示了一張學生成績表,如下所示:
DataFrame 結(jié)構(gòu)類似于 Execl 的表格型,表格中列標簽的含義如下所示:
Regd.No:表示登記的序列號Name:學生姓名Marks:學生分數(shù)
同 Series 一樣,DataFrame 自帶行標簽索引,默認為“隱式索引”即從 0 開始依次遞增,行標簽與 DataFrame 中的數(shù)據(jù)項一一對應。上述表格的行標簽從 0 到 5,共記錄了 5 條數(shù)據(jù)(圖中將行標簽省略)。當然你也可以用“顯式索引”的方式來設置行標簽。
下面對 DataFrame 數(shù)據(jù)結(jié)構(gòu)的特點做簡單地總結(jié),如下所示:
DataFrame 每一列的標簽值允許使用不同的數(shù)據(jù)類型;DataFrame 是表格型的數(shù)據(jù)結(jié)構(gòu),具有行和列;DataFrame 中的每個數(shù)據(jù)值都可以被修改。DataFrame 結(jié)構(gòu)的行數(shù)、列數(shù)允許增加或者刪除;DataFrame 有兩個方向的標簽軸,分別是行標簽和列標簽;DataFrame 可以對行和列執(zhí)行算術運算。
創(chuàng)建DataFrame對象
創(chuàng)建 DataFrame 對象的語法格式如下:
import pandas as pd pd.DataFrame( data, index, columns, dtype, copy)
參數(shù)說明:
參數(shù)名稱說明data輸入的數(shù)據(jù),可以是 ndarray,series,list,dict,標量以及一個 DataFrame。index行標簽,如果沒有傳遞 index 值,則默認行標簽是 np.arange(n),n 代表 data 的元素個數(shù)。columns列標簽,如果沒有傳遞 columns 值,則默認列標簽是 np.arange(n)。dtypedtype表示每一列的數(shù)據(jù)類型。copy默認為 False,表示復制數(shù)據(jù) data。
Pandas 提供了多種創(chuàng)建 DataFrame 對象的方式,主要包含以下五種,分別進行介紹。
1) 創(chuàng)建空的DataFrame對象
使用下列方式創(chuàng)建一個空的 DataFrame,這是 DataFrame 最基本的創(chuàng)建方法。
import pandas as pd df = pd.DataFrame() print(df)
輸出結(jié)果如下:
Empty DataFrame Columns: [] Index: []
2) 列表創(chuàng)建DataFame對象
可以使用單一列表或嵌套列表來創(chuàng)建一個 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 對象:
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 字典中,鍵對應的值的元素長度必須相同(也就是列表長度相同)。如果傳遞了索引,那么索引的長度應該等于數(shù)組的長度;如果沒有傳遞索引,那么默認情況下,索引將是 range(n),其中 n 代表數(shù)組長度。
示例 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
注意:這里使用了默認行標簽,也就是 range(n)。它生成了 0,1,2,3,并分別對應了列表中的每個元素值。
示例 5,現(xiàn)在給上述示例 4 添加自定義的行標簽:
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ù)為每行分配了一個索引。
4) 列表嵌套字典創(chuàng)建DataFrame對象
列表嵌套字典可以作為輸入數(shù)據(jù)傳遞給 DataFrame 構(gòu)造函數(shù)。默認情況下,字典的鍵被用作列名。
示例 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
注意:如果其中某個元素值缺失,也就是字典的 key 無法找到對應的 value,將使用 NaN 代替。
示例 7,給上述示例 6 添加行標簽索引:
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)建一個 DataFrame 對象。
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
注意:因為 b1 在字典鍵中不存在,所以對應值為 NaN。
5) Series創(chuàng)建DataFrame對象
您也可以傳遞一個字典形式的 Series,從而創(chuàng)建一個 DataFrame 對象,其輸出結(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
注意:對于 one 列而言,此處雖然顯示了行索引 ‘d’,但由于沒有與其對應的值,所以它的值為 NaN。
列索引操作DataFrame
DataFrame 可以使用列索(columns index)引來完成數(shù)據(jù)的選取、添加和刪除操作。下面依次對這些操作進行介紹。
1) 列索引選取數(shù)據(jù)列
您可以使用列索引,輕松實現(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 列索引表標簽可以實現(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ù)列做相加運算 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ù)列做算術運算: 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 的算術運算,這和 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
理解了上述的列索引操作后,行索引操作就變的簡單。下面看一下,如何使用行索引來選取 DataFrame 中的數(shù)據(jù)。
1) 標簽索引選取
可以將行標簽傳遞給 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 允許接兩個參數(shù)分別是行和列,參數(shù)之間需要使用“逗號”隔開,但該函數(shù)只能接收標簽索引。
2) 整數(shù)索引選取
通過將數(shù)據(jù)行所在的索引位置傳遞給 iloc 函數(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 允許接受兩個參數(shù)分別是行和列,參數(shù)之間使用“逗號”隔開,但該函數(shù)只能接收整數(shù)索引。
3) 切片操作多行選取
您也可以使用切片的方式同時選取多行。示例如下:
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ù)會在行末追加數(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ù)行
您可以使用行索引標簽,從 DataFrame 中刪除某一行數(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’]) 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
在上述的示例中,默認使用 range(2) 生成了行索引,并通過 drop(0) 同時刪除了兩行數(shù)據(jù)。
常用屬性和方法匯總
DataFrame 的屬性和方法,與 Series 相差無幾,如下所示:
名稱屬性&方法描述T行和列轉(zhuǎn)置。axes返回一個僅以行軸標簽和列軸標簽為成員的列表。dtypes返回每列數(shù)據(jù)的數(shù)據(jù)類型。emptyDataFrame中沒有數(shù)據(jù)或者任意坐標軸的長度為0,則返回True。ndim軸的數(shù)量,也指數(shù)組的維數(shù)。shape返回一個元組,表示了 DataFrame 維度。sizeDataFrame中的元素數(shù)量。values使用 numpy 數(shù)組表示 DataFrame 中的元素值。head()返回前 n 行數(shù)據(jù)。tail()返回后 n 行數(shù)據(jù)。shift()將行或列移動指定的步幅長度
下面對 DataFrame 常用屬性進行演示,首先我們創(chuàng)建一個 DataFrame 對象,示例如下:
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學苑’,‘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語言中文網(wǎng) 5 4.23 1 編程幫 6 3.24 2 百度 15 3.98 3 360搜索 28 2.56 4 谷歌 3 3.20 5 微學苑 19 4.60 6 Bing搜索 23 3.80
1) T(Transpose)轉(zhuǎn)置
返回 DataFrame 的轉(zhuǎn)置,也就是把行和列進行交換。
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學苑’,‘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語言中文網(wǎng) 編程幫 百度 360搜索 谷歌 微學苑 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
返回一個行標簽、列標簽組成的列表。
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學苑’,‘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) #輸出行、列標簽 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語言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學苑’,‘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) #輸出行、列標簽 print(df.dtypes)
輸出結(jié)果:
Name object years int64 Rating float64 dtype: object
4) empty
返回一個布爾值,判斷輸出的數(shù)據(jù)對象是否為空,若為 True 表示對象為空。
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學苑’,‘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é)果:
判斷輸入對象是否為空: False
5) ndim
返回數(shù)據(jù)對象的維數(shù)。DataFrame 是一個二維數(shù)據(jù)結(jié)構(gòu)。
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學苑’,‘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
返回一個代表 DataFrame 維度的元組。返回值元組 (a,b),其中 a 表示行數(shù),b 表示列數(shù)。
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學苑’,‘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 中的元素數(shù)量。示例如下:
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學苑’,‘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ù) 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語言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學苑’,‘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語言中文網(wǎng)’ 5 4.23] [‘編程幫’ 6 3.24] [‘百度’ 15 3.98] [‘360搜索’ 28 2.56] [‘谷歌’ 3 3.2] [‘微學苑’ 19 4.6] [‘Bing搜索’ 23 3.8]]
9) head()&tail()查看數(shù)據(jù)
如果想要查看 DataFrame 的一部分數(shù)據(jù),可以使用 head() 或者 tail() 方法。其中 head() 返回前 n 行數(shù)據(jù),默認顯示前 5 行數(shù)據(jù)。示例如下:
import pandas as pd import numpy as np d = {‘Name’:pd.Series([‘c語言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學苑’,‘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語言中文網(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語言中文網(wǎng)’,‘編程幫’,“百度”,‘360搜索’,‘谷歌’,‘微學苑’,‘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 微學苑 19 4.6 6 Bing搜索 23 3.8
10) shift()移動行或列
如果您想要移動 DataFrame 中的某一行/列,可以使用 shift() 函數(shù)實現(xiàn)。它提供了一個periods參數(shù),該參數(shù)表示在特定的軸上移動指定的步幅。
shif() 函數(shù)的語法格式如下:
DataFrame.shift(periods=1, freq=None, axis=0)
參數(shù)說明如下:
參數(shù)名稱說明peroids類型為int,表示移動的幅度,可以是正數(shù),也可以是負數(shù),默認值為1。freq日期偏移量,默認值為None,適用于時間序。取值為符合時間規(guī)則的字符串。axis如果是 0 或者 “index” 表示上下移動,如果是 1 或者 “columns” 則會左右移動。fill_value該參數(shù)用來填充缺失值。
該函數(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]}) #移動幅度為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]}) #移動幅度為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ù)不僅可以填充缺失值,還也可以對原數(shù)據(jù)進行替換。
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 是一個用來承載數(shù)據(jù)的三維數(shù)據(jù)結(jié)構(gòu),它有三個軸,分別是 items(0 軸),major_axis(1 軸),而 minor_axis(2 軸)。這三個軸為描述、操作 Panel 提供了支持,其作用介紹如下:
items:axis =0,Panel 中的每個 items 都對應一個 DataFrame。major_axis:axis=1,用來描述每個 DataFrame 的行索引。minor_axis:axis=2,用來描述每個 DataFrame 的列索引。
pandas.Panel()
您可以使用下列構(gòu)造函數(shù)創(chuàng)建一個 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默認為 False,表示是否復制數(shù)據(jù)。
創(chuàng)建Panel 對象
下面介紹創(chuàng)建 Panel 對象的兩種方式:一種是使用 nadarry 數(shù)組創(chuàng)建,另一種使用 DataFrame 對象創(chuàng)建。首先,我們學習如何創(chuàng)建一個空的 Panel 對象。
1) 創(chuàng)建一個空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 #返回均勻分布的隨機樣本值位于[0,1)之間 data = np.random.rand(2,4,5) p = pd.Panel(data) print §
輸出結(jié)果:
請注意與上述示例的空 Panel 進行對比。
3) DataFrame創(chuàng)建
下面使用 DataFrame 創(chuàng)建一個 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 對象中選取數(shù)據(jù),可以使用 Panel 的三個軸來實現(xiàn),也就是items,major_axis,minor_axis。下面介紹其中一種,大家體驗一下即可。
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,包含了兩個數(shù)據(jù)項,我們選擇了 item1,輸出結(jié)果是 4 行 3 列的 DataFrame,其行、列索引分別對應 major_axis 和 minor_axis。
Python Pandas描述性統(tǒng)計
描述統(tǒng)計學(descriptive statistics)是一門統(tǒng)計學領域的學科,主要研究如何取得反映客觀現(xiàn)象的數(shù)據(jù),并以圖表形式對所搜集的數(shù)據(jù)進行處理和顯示,最終對數(shù)據(jù)的規(guī)律、特征做出綜合性的描述分析。Pandas 庫正是對描述統(tǒng)計學知識完美應用的體現(xiàn),可以說如果沒有“描述統(tǒng)計學”作為理論基奠,那么 Pandas 是否存在猶未可知。下列表格對 Pandas 常用的統(tǒng)計學函數(shù)做了簡單的總結(jié):
函數(shù)名稱描述說明count()統(tǒng)計某個非空值的數(shù)量。sum()求和mean()求均值median()求中位數(shù)mode()求眾數(shù)std()求標準差min()求最小值max()求最大值abs()求絕對值prod()求所有數(shù)值的乘積。cumsum()計算累計和,axis=0,按照行累加;axis=1,按照列累加。cumprod()計算累計積,axis=0,按照行累積;axis=1,按照列累積。corr()計算數(shù)列或變量之間的相關系數(shù),取值-1到1,值越大表示關聯(lián)性越強。
從描述統(tǒng)計學角度出發(fā),我們可以對 DataFrame 結(jié)構(gòu)執(zhí)行聚合計算等其他操作,比如 sum() 求和、mean()求均值等方法。
在 DataFrame 中,使用聚合類方法時需要指定軸(axis)參數(shù)。下面介紹兩種傳參方式:
對行操作,默認使用 axis=0 或者使用 “index”;對列操作,默認使用 axis=1 或者使用 “columns”。
圖1:axis軸示意圖
從圖 1 可以看出,axis=0 表示按垂直方向進行計算,而 axis=1 則表示按水平方向。下面讓我們創(chuàng)建一個 DataFrame,使用它對本節(jié)的內(nèi)容進行演示。
創(chuàng)建一個 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()求和
在默認情況下,返回 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) #默認axis=0或者使用sum(“index”) print(df.sum())
輸出結(jié)果:
Name 小明小亮小紅小華老趙小曹小陳老李老王小馮小何老張 Age 382 Rating 44.92 dtype: object
注意:sum() 和 cumsum() 函數(shù)可以同時處理數(shù)字和字符串數(shù)據(jù)。雖然字符聚合通常不被使用,但使用這兩個函數(shù)并不會拋出異常;而對于 abs()、cumprod() 函數(shù)則會拋出異常,因為它們無法操作字符串數(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()求標準差
返回數(shù)值列的標準差,示例如下:
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
標準差是方差的算術平方根,它能反映一個數(shù)據(jù)集的離散程度。注意,平均數(shù)相同的兩組數(shù)據(jù),標準差未必相同。
數(shù)據(jù)匯總描述
describe() 函數(shù)顯示與 DataFrame 數(shù)據(jù)列相關的統(tǒng)計信息摘要。示例如下:
自我介紹一下,小編13年上海交大畢業(yè),曾經(jīng)在小公司待過,也去過華為、OPPO等大廠,18年進入阿里一直到現(xiàn)在。
深知大多數(shù)Go語言工程師,想要提升技能,往往是自己摸索成長或者是報班學習,但對于培訓機構(gòu)動則幾千的學費,著實壓力不小。自己不成體系的自學效果低效又漫長,而且極易碰到天花板技術停滯不前!
因此收集整理了一份《2024年Go語言全套學習資料》,初衷也很簡單,就是希望能夠幫助到想自學提升又不知道該從何學起的朋友,同時減輕大家的負擔。
既有適合小白學習的零基礎資料,也有適合3年以上經(jīng)驗的小伙伴深入學習提升的進階課程,基本涵蓋了95%以上Golang知識點,真正體系化!
由于文件比較大,這里只是將部分目錄大綱截圖出來,每個節(jié)點里面都包含大廠面經(jīng)、學習筆記、源碼講義、實戰(zhàn)項目、講解視頻,并且后續(xù)會持續(xù)更新
如果你覺得這些內(nèi)容對你有幫助,可以添加V獲?。簐ip1024b (備注Go)
一個人可以走的很快,但一群人才能走的更遠。不論你是正從事IT行業(yè)的老鳥或是對IT行業(yè)感興趣的新人,都歡迎掃碼加入我們的的圈子(技術交流、學習資源、職場吐槽、大廠內(nèi)推、面試輔導),讓我們一起學習成長!
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()求標準差
返回數(shù)值列的標準差,示例如下:
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
標準差是方差的算術平方根,它能反映一個數(shù)據(jù)集的離散程度。注意,平均數(shù)相同的兩組數(shù)據(jù),標準差未必相同。
數(shù)據(jù)匯總描述
describe() 函數(shù)顯示與 DataFrame 數(shù)據(jù)列相關的統(tǒng)計信息摘要。示例如下:
自我介紹一下,小編13年上海交大畢業(yè),曾經(jīng)在小公司待過,也去過華為、OPPO等大廠,18年進入阿里一直到現(xiàn)在。
深知大多數(shù)Go語言工程師,想要提升技能,往往是自己摸索成長或者是報班學習,但對于培訓機構(gòu)動則幾千的學費,著實壓力不小。自己不成體系的自學效果低效又漫長,而且極易碰到天花板技術停滯不前!
因此收集整理了一份《2024年Go語言全套學習資料》,初衷也很簡單,就是希望能夠幫助到想自學提升又不知道該從何學起的朋友,同時減輕大家的負擔。 [外鏈圖片轉(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)]
既有適合小白學習的零基礎資料,也有適合3年以上經(jīng)驗的小伙伴深入學習提升的進階課程,基本涵蓋了95%以上Golang知識點,真正體系化!
由于文件比較大,這里只是將部分目錄大綱截圖出來,每個節(jié)點里面都包含大廠面經(jīng)、學習筆記、源碼講義、實戰(zhàn)項目、講解視頻,并且后續(xù)會持續(xù)更新
如果你覺得這些內(nèi)容對你有幫助,可以添加V獲?。簐ip1024b (備注Go) [外鏈圖片轉(zhuǎn)存中…(img-gP12JjfX-1712980024112)]
一個人可以走的很快,但一群人才能走的更遠。不論你是正從事IT行業(yè)的老鳥或是對IT行業(yè)感興趣的新人,都歡迎掃碼加入我們的的圈子(技術交流、學習資源、職場吐槽、大廠內(nèi)推、面試輔導),讓我們一起學習成長!
柚子快報邀請碼778899分享:Pandas教程(非常詳細)
好文鏈接
本文內(nèi)容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權,聯(lián)系刪除。