python?Pandas之DataFrame索引及選取數(shù)據(jù)
1.索引是什么
1.1 認(rèn)識(shí)索引
先創(chuàng)建一個(gè)簡單的DataFrame。
myList = [['a', 10, 1.1], ['b', 20, 2.2], ['c', 30, 3.3], ['d', 40, 4.4]] df1 = pd.DataFrame(data = myList) print(df1) -------------------------------- [out]: 0 1 2 0 a 10 1.1 1 b 20 2.2 2 c 30 3.3 3 d 40 4.4
DataFrame中有兩種索引:
- 行索引(index):對應(yīng)最左邊那一豎列
- 列索引(columns):對應(yīng)最上面那一橫行
兩種索引默認(rèn)均為從0開始的自增整數(shù)。
# 輸出行索引
print(df1.index)
[out]:
RangeIndex(start=0, stop=4, step=1)
---------------------------------------
# 輸出列索引
print(df1.columns)
[out]:
RangeIndex(start=0, stop=3, step=1)
---------------------------------------
# 輸出所有的值
print(df1.values)
[out]:
array([['a', 10, 1.1],
['b', 20, 2.2],
['c', 30, 3.3],
['d', 40, 4.4]], dtype=object)1.2 自定義索引
可以使用 index 這個(gè)參數(shù)指定行索引,columns 這個(gè)參數(shù)指定列索引。
df2 = pd.DataFrame(myList,
index = ['one', 'two', 'three', 'four'],
columns = ['char', 'int', 'float'])
print(df2)
-----------------------------------------------------------
[out]:
char int float
one a 10 1.1
two b 20 2.2
three c 30 3.3
four d 40 4.4輸出此時(shí)的行索引和列索引:
# 輸出行索引
print(df2.index)
[out]:
Index(['one', 'two', 'three', 'four'], dtype='object')
--------------------------------------------------------
# 輸出列索引
print(df2.columns)
[out]:
Index(['char', 'int', 'float'], dtype='object')
2. 索引的簡單使用
2.1 列索引
選擇一列:
print(df2['char']) print(df2.char) # 兩種方式輸出一樣 [out]: one a two b three c four d Name: char, dtype: object
注意此時(shí)方括號(hào)里面只傳入一個(gè)字符串’char’,這樣選出來的一列,結(jié)果的類型為Series
print(df2['char']) print(df2.char) # 兩種方式輸出一樣 [out]: one a two b three c four d Name: char, dtype: object
選擇多列:
print(df2[['char', 'int']])
[out]:
char int
one a 10
two b 20
three c 30
four d 40注意此時(shí)方括號(hào)里面?zhèn)魅胍粋€(gè)列表 [‘char’, ‘int’],選出的結(jié)果類型為 DataFrame。
如果只想選出來一列,卻想返回 DataFrame 類型怎么辦?
print(df2[['char']])
[out]:
char
one a
two b
three c
four d
---------------------------------------
type(df2[['char']])
[out]:pandas.core.frame.DataFrame注意直接使用df2[0]取某一列會(huì)報(bào)錯(cuò),除非columns是由下標(biāo)索引組成的,比如df1那個(gè)樣子,df1[0]就不會(huì)報(bào)錯(cuò)。
print(df1[0]) [out]: 0 a 1 b 2 c 3 d Name: 0, dtype: object ----------------------- print(df2[0]) [out]: KeyError: 0
2.2 行索引
2.2.1 使用[ ]
區(qū)別于選取列,此種方式[ ]中不再單獨(dú)的傳入一個(gè)字符串,而是需要使用冒號(hào)切片。
選取行標(biāo)簽從 ’two’ 到 ’three’ 的多行數(shù)據(jù)
print(df2['two': 'three'])
[out]:
char int float
two b 20 2.2
three c 30 3.3選取行標(biāo)簽為’two’這一行數(shù)據(jù)
# 此時(shí)返回的類型為DataFrame
print(df2['two': 'two'])
[out]:
char int float
two b 20 2.2在[ ]中不僅可以傳入行標(biāo)簽,還可以傳入行的編號(hào)。
選取從第1行到第3行的數(shù)據(jù)(編號(hào)從0開始)
print(df2[1:4])
[out]:
char int float
two b 20 2.2
three c 30 3.3
four d 40 4.4可以看到選取的數(shù)據(jù)是不包含方括號(hào)最右側(cè)的編號(hào)所對應(yīng)的數(shù)據(jù)的。
選取第1行的數(shù)據(jù)
print(df2[1:2])
[out]:
char int float
two b 20 2.22.2.2 使用.loc()和.iloc()
區(qū)別就是.loc()是根據(jù)行索引和列索引的值來選取數(shù)據(jù),而.iloc()是根據(jù)從0開始的下標(biāo)位置來進(jìn)行索引的。
選取行:
使用.loc()
print(df2.loc['one'])
[out]:
char a
int 10
float 1.1
Name: one, dtype: object
-------------------------------------------
print(df2.loc[['one', 'three']])
[out]:
char int float
one a 10 1.1
three c 30 3.3使用.iloc()
print(df2.iloc[0])
[out]:
char a
int 10
float 1.1
Name: one, dtype: object
-------------------------------------------
print(df2.iloc[[0, 2]])
[out]:
char int float
one a 10 1.1
three c 30 3.3到此這篇關(guān)于python Pandas之DataFrame索引及選取數(shù)據(jù)的文章就介紹到這了,更多相關(guān)python DataFrame索引 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Alpine安裝Python3依賴出現(xiàn)的問題及解決方法
這篇文章主要介紹了Alpine安裝Python3依賴出現(xiàn)的問題及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Python threading模塊中l(wèi)ock與Rlock的使用詳細(xì)講解
python的thread模塊是比較底層的模塊,python的threading模塊是對thread做了一些包裝的,可以更加方便的被使用。這篇文章主要介紹了Python threading模塊中l(wèi)ock與Rlock的使用2022-10-10
Python基于文件內(nèi)容實(shí)現(xiàn)查找文件功能
無論是Linux系統(tǒng)還是Windows系統(tǒng)都有基于文件名實(shí)現(xiàn)過濾、查找的功能。但是如果想要查找一些關(guān)于某些文件指定內(nèi)容的文件,好像它們明面上沒有這樣的功能了。這個(gè)時(shí)候就可以通過 Python 來實(shí)現(xiàn)這樣的功能,快跟隨小編一起學(xué)習(xí)一下吧2022-05-05
Python web如何在IIS發(fā)布應(yīng)用過程解析
這篇文章主要介紹了Python web如何在IIS發(fā)布應(yīng)用過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Python實(shí)現(xiàn)識(shí)別花卉種類的示例代碼
“無窮小亮的科普日?!苯?jīng)常會(huì)發(fā)布一些鑒定網(wǎng)絡(luò)熱門生物視頻,既科普了生物知識(shí),又滿足觀眾們的獵奇心理。今天我們也來用Python鑒定一下網(wǎng)絡(luò)熱門植物2022-04-04
用Python的Django框架來制作一個(gè)RSS閱讀器
這篇文章主要介紹了用Python的Django框架來制作一個(gè)RSS閱讀器,通過url feeds來制作訂閱類應(yīng)用同樣是Django之所長,需要的朋友可以參考下2015-07-07
Python使用設(shè)計(jì)模式中的責(zé)任鏈模式與迭代器模式的示例
這篇文章主要介紹了Python使用設(shè)計(jì)模式中的責(zé)任鏈模式與迭代器模式的示例,責(zé)任鏈模式與迭代器模式都可以被看作為行為型的設(shè)計(jì)模式,需要的朋友可以參考下2016-03-03
Python reduce函數(shù)作用及實(shí)例解析
這篇文章主要介紹了Python reduce函數(shù)作用及實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05

