溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Python教程之Pandas知識(shí)點(diǎn)匯總——查詢(xún),索引,基本統(tǒng)計(jì)

發(fā)布時(shí)間:2020-08-06 21:24:48 來(lái)源:ITPUB博客 閱讀:196 作者:千鋒Python唐小強(qiáng) 欄目:編程語(yǔ)言

Python教程之Pandas知識(shí)點(diǎn)匯總——查詢(xún),索引,基本統(tǒng)計(jì)

一. 查詢(xún)與索引

1.Series和一維數(shù)組的不同:


在一維數(shù)組中就無(wú)法通過(guò)索引標(biāo)簽(index)獲取數(shù)據(jù),index默認(rèn)是從0開(kāi)始,步長(zhǎng)為1的索引,也可以自己設(shè)置索引標(biāo)簽。

2.若有兩個(gè)序列,對(duì)其進(jìn)行算術(shù)運(yùn)算,這時(shí)索引就體現(xiàn)了價(jià)值——自動(dòng)化對(duì)齊

Python教程之Pandas知識(shí)點(diǎn)匯總——查詢(xún),索引,基本統(tǒng)計(jì)
Python教程之Pandas知識(shí)點(diǎn)匯總——查詢(xún),索引,基本統(tǒng)計(jì)


由于s5、s6中存在非對(duì)應(yīng)索引,故結(jié)果存在NaN。這里的運(yùn)算過(guò)程就應(yīng)用了序列索引的自動(dòng)對(duì)齊。對(duì)于DataFrame不僅自動(dòng)對(duì)齊行,也會(huì)自動(dòng)對(duì)齊列(columns_name)。

3.DataFrame索引


DataFrame數(shù)據(jù):

Python教程之Pandas知識(shí)點(diǎn)匯總——查詢(xún),索引,基本統(tǒng)計(jì)


查詢(xún)指定行:
print(student.loc[[0,2,4,5,7]]) #這里的loc索引標(biāo)簽函數(shù)必須是中括號(hào)[ ]

Python教程之Pandas知識(shí)點(diǎn)匯總——查詢(xún),索引,基本統(tǒng)計(jì)

查詢(xún)指定列:
print(student[‘Height’].head()) #只查詢(xún)一列

Python教程之Pandas知識(shí)點(diǎn)匯總——查詢(xún),索引,基本統(tǒng)計(jì)

print(student[[‘Name’,‘Height’,‘Weight’]].head()) #如果多個(gè)列的話,必須使用雙重中括號(hào)[]

Python教程之Pandas知識(shí)點(diǎn)匯總——查詢(xún),索引,基本統(tǒng)計(jì)

print(student.loc[:,[‘Name’,‘Height’,‘Weight’]].head())

Python教程之Pandas知識(shí)點(diǎn)匯總——查詢(xún),索引,基本統(tǒng)計(jì)


按條件查詢(xún):student[(條件1) & (條件2)]

eg1: 查詢(xún)12歲以上的女生信息

print(student[(student['Sex'] == 'F') & (student['Age'] > 12)])
Python教程之Pandas知識(shí)點(diǎn)匯總——查詢(xún),索引,基本統(tǒng)計(jì)

eg2:查詢(xún)出12歲以上的女生的姓名、身高和體重

print(student[(student['Sex']=='F') & (student['Age']>12)][['Name','Height','Weight']])
Python教程之Pandas知識(shí)點(diǎn)匯總——查詢(xún),索引,基本統(tǒng)計(jì)

如果是多個(gè)條件的查詢(xún),必須使用&(and)或者丨(or)的兩端條件用括號(hào)括起來(lái)。

二. 簡(jiǎn)單的統(tǒng)計(jì)分析

Python教程之Pandas知識(shí)點(diǎn)匯總——查詢(xún),索引,基本統(tǒng)計(jì)

在實(shí)際工作中,可能處理一些數(shù)據(jù)型DataFrame,將函數(shù)應(yīng)用到DataFrame中的每一列,可以使用apply函數(shù)。

import pandas as pd 
import numpy as np 
np.random.seed(1234)
d1 = pd.Series(2*np.random.normal(size = 100)+3)
d2 = np.random.f(2,4,size = 100)
d3 = np.random.randint(1,100,size = 100)
def stats(x):
	return pd.Series(
		[x.count(),x.min(),x.idxmin(),x.quantile(.25),x.median(),x.quantile(.75),
		x.mean(),x.max(),x.idxmax(),x.mad(),x.var(),x.std(),x.skew(),x.kurt()],
		index = ['Count','Min','Whicn_Min','Q1','Median','Q3','Mean','Max','Which_Max','Mad','Var','Std','Skew','Kurt']
		)
 
 
df = pd.DataFrame(np.array([d1,d2,d3]).T,columns = ['x1','x2','x3'])
print(df.head())
print(stats(df['x1']))
print(stats(d1))
Python教程之Pandas知識(shí)點(diǎn)匯總——查詢(xún),索引,基本統(tǒng)計(jì)

講的還是比較詳細(xì)的,有什么疑問(wèn)的地方,大家可以留言,更多的 Python教程也會(huì)繼續(xù)為大家更新!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI