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

12個(gè)很棒的Pandas和NumPy函數(shù),讓分析事半功倍

大數(shù)據(jù) 數(shù)據(jù)分析
今天,小芯將分享12個(gè)很棒的Pandas和NumPy函數(shù),這些函數(shù)將會(huì)讓生活更便捷,讓分析事半功倍。

本文轉(zhuǎn)載自公眾號(hào)“讀芯術(shù)”(ID:AI_Discovery)

大家都知道Pandas和NumPy函數(shù)很棒,它們?cè)谌粘7治鲋衅鹬匾淖饔?。沒有這兩個(gè)函數(shù),人們將在這個(gè)龐大的數(shù)據(jù)分析和科學(xué)世界中迷失方向。

今天,小芯將分享12個(gè)很棒的Pandas和NumPy函數(shù),這些函數(shù)將會(huì)讓生活更便捷,讓分析事半功倍。

在本文結(jié)尾,讀者可以找到文中提到的代碼的JupyterNotebook。

數(shù)據(jù)分析/Pandas/NumPy/函數(shù)

從NumPy開始:

NumPy是使用Python進(jìn)行科學(xué)計(jì)算的基本軟件包。它包含以下內(nèi)容:

  • 強(qiáng)大的N維數(shù)組對(duì)象
  • 復(fù)雜的(廣播broadcasting)功能
  • 集成C / C++和Fortran代碼工具
  • 有用的線性代數(shù),傅立葉變換和隨機(jī)數(shù)功能

除明顯的科學(xué)用途外,NumPy是高效的通用數(shù)據(jù)多維容器,可以定義任意數(shù)據(jù)類型。這使NumPy能夠無(wú)縫且高速地與各種數(shù)據(jù)庫(kù)進(jìn)行集成。

1. allclose()

Allclose() 用于匹配兩個(gè)數(shù)組并且以布爾值形式輸出。如果兩個(gè)數(shù)組的項(xiàng)在公差范圍內(nèi)不相等,則返回False。這是檢查兩個(gè)數(shù)組是否相似的好方法,因?yàn)檫@一點(diǎn)實(shí)際很難手動(dòng)實(shí)現(xiàn)。

  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 shouldreturn 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 

2. argpartition()

NumPy的這個(gè)函數(shù)非常優(yōu)秀,可以找到N最大值索引。輸出N最大值索引,然后根據(jù)需要,對(duì)值進(jì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]) 

3. clip()

Clip() 用于將值保留在間隔的數(shù)組中。有時(shí),需要將值保持在上限和下限之間。因此,可以使用NumPy的clip()函數(shù)。給定一個(gè)間隔,該間隔以外的值都將被裁剪到間隔邊緣。

  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() 函數(shù)用于根據(jù)特定條件從數(shù)組中提取特定元素。有了該函數(shù),還可以使用and和or等的語(yǔ)句。

  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 ifremainder 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])# Applycondition on extract directly 
  10. np.extract(((array < 3) | (array > 15)), array) 
  11. array([ 0,  1, 19, 16, 18,  2]) 

5. percentile()

Percentile()用于計(jì)算沿指定軸的數(shù)組元素的第n個(gè)百分位數(shù)。

  1. a = np.array([1,5,6,8,1,7,3,6,9])print("50thPercentile 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.13.5 1.9] 

6. where()

Where() 用于從滿足特定條件的數(shù)組中返回元素。它返回在特定條件下值的索引位置。這差不多類似于在SQL中使用的where語(yǔ)句。請(qǐng)看以下示例中的演示。

  1. y = np.array([1,5,6,8,1,7,3,6,9])# Where y is greaterthan 5, returns index position 
  2. np.where(y>5) 
  3. array([2, 3, 5, 7, 8], dtype=int64),)# First will replace the values that matchthe 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'

接著來(lái)講一講神奇的Pandas函數(shù)。

Pandas

Pandas是一個(gè)Python軟件包,提供快速、靈活和富有表現(xiàn)力的數(shù)據(jù)結(jié)構(gòu),旨在使處理結(jié)構(gòu)化(表格,多維,潛在異構(gòu))的數(shù)據(jù)和時(shí)間序列數(shù)據(jù)既簡(jiǎn)單又直觀。

Pandas非常適合許多不同類型的數(shù)據(jù):

  • 具有異構(gòu)類型列的表格數(shù)據(jù),例如在SQL表或Excel電子表格中
  • 有序和無(wú)序(不一定是固定頻率)的時(shí)間序列數(shù)據(jù)。
  • 具有行和列標(biāo)簽的任意矩陣數(shù)據(jù)(同類型或異類)
  • 觀察/統(tǒng)計(jì)數(shù)據(jù)集的任何其他形式。實(shí)際上,數(shù)據(jù)根本不需要標(biāo)記,即可放入Pandas數(shù)據(jù)結(jié)構(gòu)。

以下是Pandas的優(yōu)勢(shì):

  • 輕松處理浮點(diǎn)數(shù)據(jù)和非浮點(diǎn)數(shù)據(jù)中的缺失數(shù)據(jù)(表示為NaN)
  • 大小可變性:可以從DataFrame和更高維的對(duì)象中插入和刪除列
  • 自動(dòng)和顯式的數(shù)據(jù)對(duì)齊:在計(jì)算中,可以將對(duì)象顯式對(duì)齊到一組標(biāo)簽,或者用戶可以直接忽略標(biāo)簽,并讓Series,DataFrame等自動(dòng)對(duì)齊數(shù)據(jù)
  • 強(qiáng)大靈活的分組功能,可對(duì)數(shù)據(jù)集執(zhí)行拆分-應(yīng)用-合并操作,以匯總和轉(zhuǎn)換數(shù)據(jù)
  • 輕松將其他Python和NumPy數(shù)據(jù)結(jié)構(gòu)中的不規(guī)則的、索引不同的數(shù)據(jù)轉(zhuǎn)換為DataFrame對(duì)象
  • 大數(shù)據(jù)集的智能標(biāo)簽的切片,高級(jí)索引和子集化
  • 直觀的合并和聯(lián)接數(shù)據(jù)集
  • 數(shù)據(jù)集的靈活重塑和旋
  • 坐標(biāo)軸的分層標(biāo)簽(每個(gè)刻度可能有多個(gè)標(biāo)簽)
  • 強(qiáng)大的IO工具,用于從平面文件(CSV和定界文件)、 Excel文件,數(shù)據(jù)庫(kù)加載數(shù)據(jù),以及以超高速HDF5格式保存/加載數(shù)據(jù)
  • 特定于時(shí)間序列的功能:日期范圍生成和頻率轉(zhuǎn)換、移動(dòng)窗口統(tǒng)計(jì)、日期移位和滯后。

1. apply()

Apply() 函數(shù)允許用戶傳遞函數(shù)并將其應(yīng)用于Pandas序列中每個(gè)單一值。

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

2. copy()

Copy()函數(shù)用于創(chuàng)建Pandas對(duì)象的副本。將數(shù)據(jù)幀分配給另一個(gè)數(shù)據(jù)幀時(shí),在另一個(gè)數(shù)據(jù)幀中進(jìn)行更改,其值也會(huì)進(jìn)行同步更改。為了避免出現(xiàn)上述問(wèn)題,可以使用copy()函數(shù)。

  1. # creating sample series 
  2. data = pd.Series(['India', 'Pakistan', 'China', 'Mongolia'])# Assigning issuethat 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) 

3. read_csv(nrows=n)

 

​12個(gè)很棒的Pandas和NumPy函數(shù),讓分析事半功倍
來(lái)源:Pexels

讀者可能已經(jīng)知道了read-csv函數(shù)的重要性。但即使不必要,大多數(shù)人仍會(huì)錯(cuò)誤地讀取整個(gè).csv文件。假設(shè)未知10GB的.csv文件中的列和數(shù)據(jù),在這種情況下讀取整個(gè).csv文件并非明智的決定,因?yàn)檫@會(huì)浪費(fèi)內(nèi)存和時(shí)間??梢詢H從.csv中導(dǎo)入幾行,然后根據(jù)需要繼續(xù)操作。

  1. import io 
  2. import requests# I am using this online data set just to make things easier foryou 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

4. map()

map()函數(shù)用于根據(jù)輸入對(duì)應(yīng)關(guān)系映射Series的值。用于將序列(Series)中的每個(gè)值替換為另一個(gè)值,該值可以從函數(shù)、字典或序列(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 eachfloating point value in frame 
  3. changefn = lambda x: '%.2f' % x# Make changes element-wise 
  4. dframe['d'].map(changefn) 

5. isin()

Isin() 函數(shù)用于過(guò)濾數(shù)據(jù)幀。Isin() 有助于選擇在特定列中具有特定(或多個(gè))值的行。這是筆者見過(guò)的最有用的功能。

  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] 

6. select_dtypes()

select_dtypes()函數(shù)基于dtypes列返回?cái)?shù)據(jù)框的列的子集。設(shè)置此函數(shù)的參數(shù),以包括具有某些特定數(shù)據(jù)類型的所有列;也可對(duì)其進(jìn)行設(shè)置,以排除具有某些特定數(shù)據(jù)類型的所有列。

  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。如果你糾結(jié)于是否使用groupby,并想擴(kuò)展其功能,那么不妨試試pivot-table。如果明白數(shù)據(jù)透視表在excel中的工作原理,那么一切就非常簡(jiǎn)單了。數(shù)據(jù)透視表中的級(jí)別將存貯在MultiIndex對(duì)象(分層索引)中,而該對(duì)象位于DataFrame結(jié)果的索引和列上。

  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]}) 
  5. # Lets create a pivot table to segregate students based on age and course 
  6. table = pd.pivot_table(school, values ='A'index =['B', 'C'], 
  7.                          columns =['B'], aggfunc = np.sum,fill_value="Not Available"
  8.   
  9. table 

 

責(zé)任編輯:趙寧寧 來(lái)源: 讀芯術(shù)
相關(guān)推薦

2020-03-10 08:55:50

PandasNumPy函數(shù)

2021-07-07 09:50:23

NumpyPandasPython

2024-11-18 14:20:00

ChatGPTAI

2024-01-03 18:45:35

Pandas繪圖函數(shù)

2024-01-03 14:54:56

PythonPandas數(shù)據(jù)處理工具

2018-11-19 15:06:23

Python庫(kù)算法

2023-04-10 14:49:35

Web應(yīng)用程序工具

2023-12-22 15:44:43

2011-04-21 13:02:29

2023-08-30 09:16:38

PandasPython

2023-09-08 13:11:00

NumPyPandasPython庫(kù)

2019-11-01 10:49:21

技術(shù)開源應(yīng)用

2023-07-31 11:44:38

Pandas性能數(shù)組

2023-10-15 17:07:35

PandasPython庫(kù)

2024-01-05 09:13:35

2022-09-20 10:50:34

PandasNumPy

2023-06-08 14:10:00

VSCodePython代碼

2022-07-06 23:59:57

NumPyPython工具

2023-10-04 19:38:01

插件主題IntelliJ

2010-07-16 10:23:28

Batch telne
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 国产精品免费大片 | 久久久一区二区三区 | 午夜网 | 99精品欧美一区二区三区综合在线 | 精品视频在线一区 | 国产精品国产自产拍高清 | 这里只有精品99re | 亚洲视频精品在线 | 91精品国产综合久久精品图片 | 日韩在线精品 | 精品久久久精品 | 天天天天天操 | 欧美在线 | 国产一区二区三区在线 | 久久一区二 | 欧美成人激情视频 | 中文字幕亚洲精品在线观看 | 狠狠骚 | 亚洲欧美日韩在线 | 超碰97免费 | 亚洲高清视频在线 | 91精品国产综合久久婷婷香蕉 | 日日操夜夜操天天操 | 精品欧美黑人一区二区三区 | 天天躁日日躁狠狠躁白人 | 久久精品亚洲欧美日韩精品中文字幕 | 羞羞视频免费在线观看 | 精精国产xxxx视频在线 | 久久蜜桃资源一区二区老牛 | 国产免费播放视频 | 美女久久久久久久久 | 91精品久久久久 | 亚洲视频一区二区三区 | 久久综合一区二区 | 91视频电影 | 久久不卡日韩美女 | 日日操操 | 在线免费观看视频你懂的 | 欧洲性生活视频 | 精品一区二区三区免费毛片 | 伊人伊成久久人综合网站 |