欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pandas DataFrame 數(shù)據(jù)選取,修改,切片的實(shí)現(xiàn)

 更新時(shí)間:2020年04月24日 10:41:59   作者:yoonhee  
這篇文章主要介紹了pandas DataFrame 數(shù)據(jù)選取,修改,切片的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

在剛開始使用pandas DataFrame的時(shí)候,對于數(shù)據(jù)的選取,修改和切片經(jīng)常困惑,這里總結(jié)了一些常用的操作。

pandas主要提供了三種屬性用來選取行/列數(shù)據(jù):

屬性名 屬性
ix 根據(jù)整數(shù)索引或者行標(biāo)簽選取數(shù)據(jù)
iloc 根據(jù)位置的整數(shù)索引選取數(shù)據(jù)
loc 根據(jù)行標(biāo)簽選取數(shù)據(jù)

先初始化一個(gè)DateFrame做例子

import numpy as np
import pandas as pd
df = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])

df是這樣的

In [35]: df
Out[35]: 
   name  gender age
0  Snow   M    22
1  Tyrion  M    32
2  Sansa  F    18
3  Arya   F    14

如果每列都有column name,比如這里是: 

In [42]: df.columns
Out[42]: Index(['name', 'gender', 'age'], dtype='object')

1. df['column_name'] ,df[row_start_index, row_end_index] 選取指定整列數(shù)據(jù)

df['name']
df['gender']
df[['name','gender']] #選取多列,多列名字要放在list里
df[0:] #第0行及之后的行,相當(dāng)于df的全部數(shù)據(jù),注意冒號是必須的
df[:2] #第2行之前的數(shù)據(jù)(不含第2行)
df[0:1] #第0行
df[1:3] #第1行到第2行(不含第3行)
df[-1:] #最后一行
df[-3:-1] #倒數(shù)第3行到倒數(shù)第1行(不包含最后1行即倒數(shù)第1行,這里有點(diǎn)煩躁,因?yàn)閺那皵?shù)時(shí)從第0行開始,從后數(shù)就是-1行開始,畢竟沒有-0)

2. loc,在知道列名字的情況下,df.loc[index,column] 選取指定行,列的數(shù)據(jù)

loc也提供了五種參數(shù)形式

input example(摘自官方文檔) output
行標(biāo)簽 df.loc[‘viper'] 選取viper那一行
行標(biāo)簽數(shù)組 df.loc[[‘viper', ‘sidewinder']] 選取行標(biāo)簽為viper、sidewinder
行標(biāo)簽切片 df.loc[‘cobra':‘viper', ‘max_speed'] 選取從cobra到viper行的max_speed列
布爾值數(shù)組 df.loc[[False, False, True]]
函數(shù) df.loc[df[‘shield'] > 6, [‘max_speed']] 選取shield列大于6的那一行的max_speed數(shù)據(jù)

注意 df.loc[df[‘one']>10]這樣的寫法是可以正常選出one列大于10的數(shù)據(jù)

# df.loc[index, column_name],選取指定行和列的數(shù)據(jù)
df.loc[0,'name'] # 'Snow'
df.loc[0:2, ['name','age']]  #選取第0行到第2行,name列和age列的數(shù)據(jù), 注意這里的行選取是包含下標(biāo)的。
df.loc[[2,3],['name','age']]  #選取指定的第2行和第3行,name和age列的數(shù)據(jù)
df.loc[df['gender']=='M','name']  #選取gender列是M,name列的數(shù)據(jù)
df.loc[df['gender']=='M',['name','age']] #選取gender列是M,name和age列的數(shù)據(jù)

3. iloc,在column name特別長或者index是時(shí)間序列等各種不方便輸入的情況下,可以用iloc (i = index), iloc完全用數(shù)字來定位 iloc[row_index, column_index]

iloc提供了五種參數(shù)形式

input example output
整數(shù)(行索引) df.iloc[5] 選取第6行數(shù)據(jù)
整數(shù)數(shù)組 df.iloc[[1,3,5]] 選取第2,4,6行數(shù)據(jù)
整數(shù)切片 df.iloc[1:3] 選取2~4行數(shù)據(jù)(不包含第4行數(shù)據(jù))
布爾值數(shù)組 df.iloc[[True,False,True] 選取第1,3行數(shù)據(jù)
函數(shù) df.iloc[(df[‘one']>10).tolist()] 選取'one'這列大于10的那一行數(shù)據(jù)

注意:iloc接受有返回值的函數(shù)作為參數(shù),但要保證函數(shù)返回的是整數(shù)/整數(shù)list,布爾值/布爾list

如果直接運(yùn)行 df.iloc[df[‘one']>10]

則會報(bào)錯(cuò) NotImplementedError: iLocation based boolean indexing on an integer type is not available

因?yàn)閐f[‘one'] > 10 返回的是 series類型的數(shù)據(jù)

除此之外,還可以進(jìn)行組合切片

input example output
整數(shù)(行索引) df.iloc[5,1] 選取第6行,第2列的數(shù)據(jù)
整數(shù)數(shù)組 df.iloc[[1,3],[1,2]] 選取第2,4行;2,3列的數(shù)據(jù)
整數(shù)切片 df.iloc[1:3,1:3] 選取第2,3行;2,3列的數(shù)據(jù)
布爾值數(shù)組 df.iloc[[True,True,False],[True,False,True]] 選取第1,2行;1,3列的數(shù)據(jù)

要注意的是,我們用df[參數(shù)]也可以進(jìn)行切片,但這種方式容易引起chained indexing 問題。除此之外,**df[lable1][lable2]**的操作是線性的,對lable2的選取是在df[lable1]的基礎(chǔ)上進(jìn)行,速度相對較慢。所以在對數(shù)據(jù)進(jìn)行切片的時(shí)候盡量使用iloc這類的方法

df.iloc[0,0] #第0行第0列的數(shù)據(jù),'Snow'
df.iloc[1,2] #第1行第2列的數(shù)據(jù),32
df.iloc[[1,3],0:2] #第1行和第3行,從第0列到第2列(不包含第2列)的數(shù)據(jù)
df.iloc[1:3,[1,2] #第1行到第3行(不包含第3行),第1列和第2列的數(shù)據(jù)

4. ix, ix很強(qiáng)大,loc和iloc的功能都能做到 ix[row_index, column_index]

ix雖然強(qiáng)大,然而已經(jīng)不再被推薦,因?yàn)樵谧钚掳娴膒andas里面,ix已經(jīng)成為deprecated。(https://github.com/pandas-dev/pandas/issues/14218

大概是因?yàn)榭梢曰旌蟣abel和position導(dǎo)致了很多用戶問題和bug。

所以,用label就用loc,用position就用iloc。

df.ix[0,0] #第0行第0列的數(shù)據(jù),'Snow'
df.ix[0,[1,2]] #第0行,第1列和第2列的數(shù)據(jù)
df.ix[0:2,[1,2]] #第0行到第2行(包含第3行),第1列和第2列的數(shù)據(jù)
df.ix[1,0:2] #第1行,從第0列到第2列(不包含第2列)的數(shù)據(jù)

切片時(shí),iloc行不含下標(biāo)上限,loc,ix行包含,列iloc和ix都不含列下標(biāo)上限。(設(shè)計(jì)者的缺憾。。。)

 到此這篇關(guān)于pandas DataFrame 數(shù)據(jù)選取,修改,切片的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)pandas  數(shù)據(jù)選取,修改,切片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論