網站首頁 編程語言 正文
處理數據時,經常需要對索引進行處理,那么可以通過set_index和reset_index來進行處理
官方文檔
DataFrame.set_index(self, keys, drop=True, append=False, inplace=False, verify_integrity=False)
參數解釋
構建實例
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
需要設置成索引的數據,可以使一個標簽,數組,或者標簽或數組的列表
df.set_index('name')#指定某一列為索引
?? ?height?? ?weight
name?? ??? ?
小王?? ?178?? ??? ?156
小明?? ?171?? ??? ?90
小綠?? ?185?? ??? ?140
小紅?? ?196?? ??? ?142
drop:bool,default True
是否刪除作為索引使用的列,默認True,即刪除做為索引的列
df.set_index('name',drop=False)
?? ??? ?height?? ?weight?? ?name
name?? ??? ??? ?
小王?? ?178?? ??? ?156?? ??? ?小王
小明?? ?171?? ??? ?90?? ??? ?小明
小綠?? ?185?? ??? ?140?? ??? ?小綠
小紅?? ?196?? ??? ?142?? ??? ?小紅
append:bool default False
將序列添加到索引中,形成多級序列
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
將結果返回為原變量
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#無需對df重新賦值,df即為上邊代碼的結果
?? ??? ??? ?height?? ?weight?? ?name
?? ?name?? ??? ??? ?
0?? ?小王?? ?178?? ??? ?156?? ??? ?小王
1?? ?小明?? ?171?? ??? ?90?? ??? ?小明
2?? ?小綠?? ?185?? ??? ?140?? ??? ?小綠
3?? ?小紅?? ?196?? ??? ?142?? ??? ?小紅
verify_integrity:bool default False
檢查索引是否重復。默認是False。
原文鏈接:https://blog.csdn.net/lisnyuan/article/details/107086094
相關推薦
- 2022-08-30 C++示例講解初始化列表方法_C 語言
- 2022-08-22 Golang實現快速求冪的方法詳解_Golang
- 2023-02-03 python關于excel多個sheet的導入導出方式_python
- 2022-03-03 gyp ERR! configure error. gyp ERR! stack Error: gy
- 2024-01-08 Spring AOP 切面@Around注解的具體使用
- 2023-06-16 Dubbo?系列JDK?SPI?原理解析_服務器其它
- 2022-07-09 ABP基礎架構深入探索_基礎應用
- 2022-05-17 docker?--link容器互聯的實現_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支