pandas學(xué)習(xí)之df.set_index的具體使用
處理數(shù)據(jù)時(shí),經(jīng)常需要對(duì)索引進(jìn)行處理,那么可以通過(guò)set_index和reset_index來(lái)進(jìn)行處理
官方文檔
DataFrame.set_index(self, keys, drop=True, append=False, inplace=False, verify_integrity=False)
參數(shù)解釋
構(gòu)建實(shí)例
import pandas as pd df = pd.DataFrame(data={'height':[178,171,185,196],'weight':[156,90,140,142], ?? ??? ??? ??? ??? ??? ?'name':['小王','小明','小綠','小紅']}) df ?? ?height?? ?weight?? ?name 0?? ?178?? ??? ?156?? ??? ?小王 1?? ?171?? ??? ?90?? ??? ?小明 2?? ?185?? ??? ?140?? ??? ?小綠 3?? ?196?? ??? ?142?? ??? ?小紅
key:label array-like or list of label/arrays
需要設(shè)置成索引的數(shù)據(jù),可以使一個(gè)標(biāo)簽,數(shù)組,或者標(biāo)簽或數(shù)組的列表
df.set_index('name')#指定某一列為索引 ?? ?height?? ?weight name?? ??? ? 小王?? ?178?? ??? ?156 小明?? ?171?? ??? ?90 小綠?? ?185?? ??? ?140 小紅?? ?196?? ??? ?142
drop:bool,default True
是否刪除作為索引使用的列,默認(rèn)True,即刪除做為索引的列
df.set_index('name',drop=False) ?? ??? ?height?? ?weight?? ?name name?? ??? ??? ? 小王?? ?178?? ??? ?156?? ??? ?小王 小明?? ?171?? ??? ?90?? ??? ?小明 小綠?? ?185?? ??? ?140?? ??? ?小綠 小紅?? ?196?? ??? ?142?? ??? ?小紅
append:bool default False
將序列添加到索引中,形成多級(jí)序列
df.set_index(df['name'],append = True) ? ? ? ? ? ? height?? ?weight?? ?name ?? ?name?? ??? ??? ? 0?? ?小王?? ?178?? ??? ?156?? ??? ?小王 1?? ?小明?? ?171?? ??? ?90?? ??? ?小明 2?? ?小綠?? ?185?? ??? ?140?? ??? ?小綠 3?? ?小紅?? ?196?? ??? ?142?? ??? ?小紅 # 前兩列都為索引
inplace:bool default False
將結(jié)果返回為原變量
df#原df ?? ?height?? ?weight?? ?name 0?? ?178?? ??? ?156?? ??? ?小王 1?? ?171?? ??? ?90?? ??? ?小明 2?? ?185?? ??? ?140?? ??? ?小綠 3?? ?196?? ??? ?142?? ??? ?小紅 df.set_index(df['name'],append = True,inplace = True) ?? ??? ??? ?height?? ?weight?? ?name ?? ?name?? ??? ??? ? 0?? ?小王?? ?178?? ??? ?156?? ??? ?小王 1?? ?小明?? ?171?? ??? ?90?? ??? ?小明 2?? ?小綠?? ?185?? ??? ?140?? ??? ?小綠 3?? ?小紅?? ?196?? ??? ?142?? ??? ?小紅 df#無(wú)需對(duì)df重新賦值,df即為上邊代碼的結(jié)果 ?? ??? ??? ?height?? ?weight?? ?name ?? ?name?? ??? ??? ? 0?? ?小王?? ?178?? ??? ?156?? ??? ?小王 1?? ?小明?? ?171?? ??? ?90?? ??? ?小明 2?? ?小綠?? ?185?? ??? ?140?? ??? ?小綠 3?? ?小紅?? ?196?? ??? ?142?? ??? ?小紅
verify_integrity:bool default False
檢查索引是否重復(fù)。默認(rèn)是False。
到此這篇關(guān)于pandas學(xué)習(xí)之df.set_index的具體使用的文章就介紹到這了,更多相關(guān)pandas df.set_index內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- pandas 強(qiáng)制類(lèi)型轉(zhuǎn)換 df.astype實(shí)例
- pandas || df.dropna() 缺失值刪除操作
- pandas數(shù)據(jù)選?。篸f[] df.loc[] df.iloc[] df.ix[] df.at[] df.iat[]
- Pandas數(shù)據(jù)形狀df.shape的實(shí)現(xiàn)
- Pandas索引排序 df.sort_index()的實(shí)現(xiàn)
- Pandas缺失值刪除df.dropna()的使用
- Pandas數(shù)據(jù)類(lèi)型轉(zhuǎn)換df.astype()及數(shù)據(jù)類(lèi)型查看df.dtypes的使用
- Pandas中df.loc[]與df.iloc[]的用法與異同?
相關(guān)文章
django框架使用views.py的函數(shù)對(duì)表進(jìn)行增刪改查內(nèi)容操作詳解【models.py中表的創(chuàng)建、views.py中
這篇文章主要介紹了django框架使用views.py的函數(shù)對(duì)表進(jìn)行增刪改查內(nèi)容操作,結(jié)合實(shí)例形式詳細(xì)分析了models.py中表的創(chuàng)建、views.py中函數(shù)的使用,基于對(duì)象的跨表查詢等相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2019-12-12python迷宮問(wèn)題深度優(yōu)先遍歷實(shí)例
這篇文章主要給大家介紹了關(guān)于python迷宮問(wèn)題深度優(yōu)先遍歷的相關(guān)資料,深度優(yōu)先搜索算法(Depth-First-Search),是搜索算法的一種,需要的朋友可以參考下2021-06-06Python?ctypes庫(kù)底層交互秘籍實(shí)例探究
ctypes是Python標(biāo)準(zhǔn)庫(kù)中的外部函數(shù)庫(kù),允許Python調(diào)用動(dòng)態(tài)鏈接庫(kù)中的函數(shù),它提供了與C兼容的數(shù)據(jù)類(lèi)型和允許Python調(diào)用共享庫(kù)中的函數(shù),對(duì)系統(tǒng)級(jí)編程和與硬件交互非常有用2024-01-01Pytorch中torch.cat()函數(shù)的使用及說(shuō)明
這篇文章主要介紹了Pytorch中torch.cat()函數(shù)的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01詳解Appium+Python之生成html測(cè)試報(bào)告
這篇文章主要介紹了詳解Appium+Python之生成html測(cè)試報(bào)告,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01利用Python進(jìn)行圖像的加法,圖像混合(附代碼)
這篇文章主要介紹了利用Python進(jìn)行圖像的加法,圖像混合(附代碼),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07python區(qū)塊鏈持久化和命令行接口實(shí)現(xiàn)簡(jiǎn)版
這篇文章主要為大家介紹了python區(qū)塊鏈持久化和命令行接口實(shí)現(xiàn)簡(jiǎn)版,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05