一. 查詢與索引
1.Series和一維數(shù)組的不同:
在一維數(shù)組中就無法通過索引標(biāo)簽(index)獲取數(shù)據(jù),index默認(rèn)是從0開始,步長為1的索引,也可以自己設(shè)置索引標(biāo)簽。
2.若有兩個(gè)序列,對(duì)其進(jìn)行算術(shù)運(yùn)算,這時(shí)索引就體現(xiàn)了價(jià)值——自動(dòng)化對(duì)齊
由于s5、s6中存在非對(duì)應(yīng)索引,故結(jié)果存在NaN。這里的運(yùn)算過程就應(yīng)用了序列索引的自動(dòng)對(duì)齊。對(duì)于DataFrame不僅自動(dòng)對(duì)齊行,也會(huì)自動(dòng)對(duì)齊列(columns_name)。
3.DataFrame索引
DataFrame數(shù)據(jù):
查詢指定行:
print(student.loc[[0,2,4,5,7]]) #這里的loc索引標(biāo)簽函數(shù)必須是中括號(hào)[ ]
查詢指定列:
print(student[‘Height’].head()) #只查詢一列
print(student[[‘Name’,‘Height’,‘Weight’]].head()) #如果多個(gè)列的話,必須使用雙重中括號(hào)[]
print(student.loc[:,[‘Name’,‘Height’,‘Weight’]].head())
按條件查詢:student[(條件1) & (條件2)]
eg1: 查詢12歲以上的女生信息
print(student[(student['Sex'] == 'F') & (student['Age'] > 12)])
eg2:查詢出12歲以上的女生的姓名、身高和體重
print(student[(student['Sex']=='F') & (student['Age']>12)][['Name','Height','Weight']])
如果是多個(gè)條件的查詢,必須使用&(and)或者丨(or)的兩端條件用括號(hào)括起來。
二. 簡(jiǎ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))
聯(lián)系客服