成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

12個Numpy與Pandas高效技巧,讓數據分析更容易

開發 后端
本文分享給大家 12 種 Numpy 和 Pandas 函數,這些高效的函數會令數據分析更為容易、便捷。最后,讀者也可以在 GitHub 項目中找到本文所用代碼的 Jupyter Notebook。

 本文分享給大家 12 種 Numpy 和 Pandas 函數,這些高效的函數會令數據分析更為容易、便捷。最后,讀者也可以在 GitHub 項目中找到本文所用代碼的 Jupyter Notebook。

項目地址:https://github.com/kunaldhariwal/12-Amazing-Pandas-NumPy-Functions

Numpy 的 6 種高效函數

首先從 Numpy 開始。Numpy 是用于科學計算的 Python 語言擴展包,通常包含強大的 N 維數組對象、復雜函數、用于整合 C/C++和 Fortran 代碼的工具以及有用的線性代數、傅里葉變換和隨機數生成能力。

除了上面這些明顯的用途,Numpy 還可以用作通用數據的高效多維容器(container),定義任何數據類型。這使得 Numpy 能夠實現自身與各種數據庫的無縫、快速集成。

接下來一一解析 6 種 Numpy 函數。

1、argpartition()

借助于 argpartition(),Numpy 可以找出 N 個最大數值的索引,也會將找到的這些索引輸出。然后我們根據需要對數值進行排序。 

  1. x = np.array([12, 10, 12, 0, 6, 8, 9, 1, 16, 4, 6, 0])index_val = np.argpartition(x, -4)[-4:]  
  2. index_val  
  3. array([1, 8, 2, 0], dtype=int64)np.sort(x[index_val])  
  4. array([10, 12, 12, 16]) 

2、allclose()

allclose() 用于匹配兩個數組,并得到布爾值表示的輸出。如果在一個公差范圍內(within a tolerance)兩個數組不等同,則 allclose() 返回 False。該函數對于檢查兩個數組是否相似非常有用。 

  1. array1 = np.array([0.12,0.17,0.24,0.29])  
  2. array2 = np.array([0.13,0.19,0.26,0.31])# with a tolerance of 0.1, it should return False:  
  3. np.allclose(array1,array2,0.1)  
  4. False# with a tolerance of 0.2, it should return True: 
  5. np.allclose(array1,array2,0.2)  
  6. True 

3、clip()

Clip() 使得一個數組中的數值保持在一個區間內。有時,我們需要保證數值在上下限范圍內。為此,我們可以借助 Numpy 的 clip() 函數實現該目的。給定一個區間,則區間外的數值被剪切至區間上下限(interval edge)。 

  1. x = np.array([3, 17, 14, 23, 2, 2, 6, 8, 1, 2, 16, 0])np.clip(x,2,5)  
  2. array([3, 5, 5, 5, 2, 2, 5, 5, 2, 2, 5, 2]) 

4、extract()

顧名思義,extract() 是在特定條件下從一個數組中提取特定元素。借助于 extract(),我們還可以使用 and 和 or 等條件。 

  1. # Random integers  
  2. array = np.random.randint(20, size=12 
  3. array  
  4. array([ 0,  1,  8, 19, 16, 18, 10, 11,  2, 13, 14,  3])#  Divide by 2 and check if remainder is 1  
  5. cond = np.mod(array, 2)==1  
  6. cond  
  7. array([False,  True, False,  True, False, False, False,  True, False, True, False,  True])# Use extract to get the values  
  8. np.extract(cond, array)  
  9. array([ 1, 19, 11, 13,  3])# Apply condition on extract directly  
  10. np.extract(((array < 3) | (array > 15)), array)  
  11. array([ 0,  1, 19, 16, 18,  2]) 

5、where()

Where() 用于從一個數組中返回滿足特定條件的元素。比如,它會返回滿足特定條件的數值的索引位置。Where() 與 SQL 中使用的 where condition 類似,如以下示例所示: 

  1. y = np.array([1,5,6,8,1,7,3,6,9])# Where y is greater than 5, returns index position  
  2. np.where(y>5)  
  3. array([2, 3, 5, 7, 8], dtype=int64),)# First will replace the values that match the condition,   
  4. # second will replace the values that does not  
  5. np.where(y>5, "Hit", "Miss")  
  6. array([ Miss ,  Miss ,  Hit ,  Hit ,  Miss ,  Hit ,  Miss ,  Hit ,  Hit ],dtype<U4 ) 

6、percentile()

Percentile() 用于計算特定軸方向上數組元素的第 n 個百分位數。 

  1. a = np.array([1,5,6,8,1,7,3,6,9])print("50th Percentile of a, axis = 0 : ",    
  2.       np.percentile(a, 50, axis =0))  
  3. 50th Percentile of a, axis = 0 :  6.0b = np.array([[10, 7, 4], [3, 2, 1]])print("30th Percentile of b, axis = 0 : ",    
  4.       np.percentile(b, 30, axis =0))  
  5. 30th Percentile of b, axis = 0 :  [5.1 3.5 1.9] 

這就是 Numpy 擴展包的 6 種高效函數,相信會為你帶來幫助。接下來看一看 Pandas 數據分析庫的 6 種函數。

Pandas的 6 種高效函數

Pandas 也是一個 Python 包,它提供了快速、靈活以及具有顯著表達能力的數據結構,旨在使處理結構化 (表格化、多維、異構) 和時間序列數據變得既簡單又直觀。

Pandas 適用于以下各類數據:

  •  具有異構類型列的表格數據,如 SQL 表或 Excel 表;
  •  有序和無序 (不一定是固定頻率) 的時間序列數據;
  •  帶有行/列標簽的任意矩陣數據(同構類型或者是異構類型);
  •  其他任意形式的統計數據集。事實上,數據根本不需要標記就可以放入 Pandas 結構中。

1、read_csv(nrows=n)

大多數人都會犯的一個錯誤是,在不需要.csv 文件的情況下仍會完整地讀取它。如果一個未知的.csv 文件有 10GB,那么讀取整個.csv 文件將會非常不明智,不僅要占用大量內存,還會花很多時間。我們需要做的只是從.csv 文件中導入幾行,之后根據需要繼續導入。 

  1. import io  
  2. import requests# I am using this online data set just to make things easier for you guys  
  3. url = "https://raw.github.com/vincentarelbundock/Rdatasets/master/csv/datasets/AirPassengers.csv"  
  4. s = requests.get(url).content# read only first 10 rows  
  5. df = pd.read_csv(io.StringIO(s.decode( utf-8 )),nrows=10 , index_col=0

2、map()

map( ) 函數根據相應的輸入來映射 Series 的值。用于將一個 Series 中的每個值替換為另一個值,該值可能來自一個函數、也可能來自于一個 dict 或 Series。 

  1. # create a dataframe  
  2. dframe = pd.DataFrame(np.random.randn(4, 3), columns=list( bde ), index=[ India ,  USA ,  China ,  Russia ])#compute a formatted string from each floating point value in frame 
  3. changefn = lambda x:  %.2f  % x# Make changes element-wise 
  4. dframe[ d ].map(changefn) 

3、apply()

apply() 允許用戶傳遞函數,并將其應用于 Pandas 序列中的每個值。 

  1. # max minus mix lambda fn  
  2. fn = lambda x: x.max() - x.min()# Apply this on dframe that we ve just created above  
  3. dframe.apply(fn) 

4、isin()

lsin () 用于過濾數據幀。Isin () 有助于選擇特定列中具有特定(或多個)值的行。 

  1. # Using the dataframe we created for read_csv  
  2. filter1 = df["value"].isin([112])   
  3. filter2 = df["time"].isin([1949.000000])df [filter1 & filter2] 

5、copy()

Copy () 函數用于復制 Pandas 對象。當一個數據幀分配給另一個數據幀時,如果對其中一個數據幀進行更改,另一個數據幀的值也將發生更改。為了防止這類問題,可以使用 copy () 函數。 

  1. # creating sample series   
  2. data = pd.Series([ India ,  Pakistan ,  China ,  Mongolia ])# Assigning issue that we face  
  3. datadata1= data  
  4. # Change a value  
  5. data1[0]= USA   
  6. # Also changes value in old dataframe  
  7. data# To prevent that, we use  
  8. # creating copy of series   
  9. new = data.copy()# assigning new values   
  10. new[1]= Changed value # printing data   
  11. print(new)   
  12. print(data) 

6、select_dtypes()

select_dtypes() 的作用是,基于 dtypes 的列返回數據幀列的一個子集。這個函數的參數可設置為包含所有擁有特定數據類型的列,亦或者設置為排除具有特定數據類型的列。 

  1. # We ll use the same dataframe that we used for read_csv  
  2. framex =  df.select_dtypes(include="float64")# Returns only time column 

最后,pivot_table( ) 也是 Pandas 中一個非常有用的函數。如果對 pivot_table( ) 在 excel 中的使用有所了解,那么就非常容易上手了。 

  1. # Create a sample dataframe  
  2. school = pd.DataFrame({ A : [ Jay ,  Usher ,  Nicky ,  Romero ,  Will ],   
  3.        B : [ Masters ,  Graduate ,  Graduate ,  Masters ,  Graduate ],   
  4.        C : [26, 22, 20, 23, 24]})# Lets create a pivot table to segregate students based on age and course  
  5. table = pd.pivot_table(school, values = A , index =[ B ,  C ],   
  6.                          columns =[ B ], aggfunc = np.sum, fill_value="Not Available")   
  7. table  

 

責任編輯:龐桂玉 來源: Linux公社
相關推薦

2020-04-03 13:50:19

數據分析PandasNumPy

2021-12-24 10:45:19

PandasLambda數據分析

2018-08-23 17:15:10

編程語言Python數據分析

2021-02-19 10:59:29

NumpyPandasPython

2009-10-27 09:09:06

Eclipse技巧

2024-01-03 14:54:56

PythonPandas數據處理工具

2022-07-08 06:01:37

D-Tale輔助工具

2022-09-20 10:50:34

PandasNumPy

2020-03-10 08:55:50

PandasNumPy函數

2016-11-18 15:42:06

存儲

2023-10-04 00:17:00

SQL數據庫

2025-04-16 08:10:00

PandasPython數據分析

2017-12-27 11:38:14

數據分析大數據算法

2019-09-10 11:31:16

Python數據分析表達式

2020-03-19 15:11:14

Pandas數據分析代碼

2025-06-06 08:35:41

2024-06-06 09:08:14

NumPyPython數據分析

2022-04-01 06:18:48

數據分析IT領導者

2019-04-29 08:31:25

PythonPandas數據

2019-09-11 14:34:13

排序算法數據科學
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩精品视频在线 | 伦理片97 | 国产精品视频一区二区三区 | 国产综合精品一区二区三区 | 欧美在线视频网站 | 不卡一区 | 日韩在线播放视频 | 天天色图| 欧美色综合一区二区三区 | 成人一级黄色毛片 | 国产99久久精品 | 国产综合久久 | 一区二区三 | 婷婷久久精品一区二区 | 91精品久久久久久久久 | 中文字幕 国产 | 国产精品福利视频 | 精品久久久久久国产 | 99爱视频 | 在线激情视频 | www.亚洲 | 日韩小视频在线 | 久久久久国产精品午夜一区 | 国产乱码精品一区二区三区av | 天天躁日日躁xxxxaaaa | 成人国产在线观看 | 亚洲综合色视频在线观看 | 精品国产亚洲一区二区三区大结局 | 一区中文字幕 | 玖玖视频 | 在线免费观看毛片 | 日韩成人在线网站 | 中文字幕成人在线 | 精品九九九 | 中文字幕视频在线观看 | 国产真实乱对白精彩久久小说 | 精品在线一区 | 美国av片在线观看 | 超碰av人人 | 国产一区二区三区在线 | 亚洲一区二区三区四区五区中文 |