日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Pandas (GeoPandas)笔记:set_index reset_index

發(fā)布時(shí)間:2025/4/5 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pandas (GeoPandas)笔记:set_index reset_index 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

python包介紹:GeoPandas(初識)_UQI-LIUWJ的博客-CSDN博客

python 庫整理:pandas_UQI-LIUWJ的博客-CSDN博客

1 set_index

將?DataFrame?中的列轉(zhuǎn)化為行索引。

默認(rèn)情況下,轉(zhuǎn)換之后,原來的列將不見

1.1 基本使用方法

DataFrame.set_index(keys, drop=True, append=False, inplace=False, verify_integrity=False)

1.2 參數(shù)說明

keys列標(biāo)簽(需要設(shè)置為索引的列)
drop

刪除用作新索引的列

默認(rèn)為True

append

是否將列附加到現(xiàn)有索引

默認(rèn)為False

inplace

表示當(dāng)前操作是否對原數(shù)據(jù)生效

默認(rèn)為False

verify_integrity

檢查新索引的副本

將其設(shè)置為False將提高該方法的性能

默認(rèn)為false

1.3 舉例說明

1.3.0 原始數(shù)據(jù)

import pandas as pddata={'a':range(7),'b':range(7,0,-1),'c':['one','one','one','two','two','two','two'],'d':[0,1,2,0,1,2,3]} f1=pd.DataFrame(data) f1'''a b c d 0 0 7 one 0 1 1 6 one 1 2 2 5 one 2 3 3 4 two 0 4 4 3 two 1 5 5 2 two 2 6 6 1 two 3 '''

1.3.1 默認(rèn)情況

f1.set_index(['c']) '''a b d c one 0 7 0 one 1 6 1 one 2 5 2 two 3 4 0 two 4 3 1 two 5 2 2 two 6 1 3 '''

1.3.2 保留索引所在的列(drop)

f1.set_index(['c'],drop=False) '''a b c d c one 0 7 one 0 one 1 6 one 1 one 2 5 one 2 two 3 4 two 0 two 4 3 two 1 two 5 2 two 2 two 6 1 two 3 '''

?1.3.3 多重索引

f1.set_index(['c','d'], drop=False) '''a b c d c d one 0 0 7 one 01 1 6 one 12 2 5 one 2 two 0 3 4 two 01 4 3 two 12 5 2 two 23 6 1 two 3 '''f1.set_index(['d','c'], drop=False) '''a b c d d c 0 one 0 7 one 0 1 one 1 6 one 1 2 one 2 5 one 2 0 two 3 4 two 0 1 two 4 3 two 1 2 two 5 2 two 2 3 two 6 1 two 3 '''

1.3.4 添加到原有索引(append)

f1.set_index('c', append=True) '''a b dc 0 one 0 7 0 1 one 1 6 1 2 one 2 5 2 3 two 3 4 0 4 two 4 3 1 5 two 5 2 2 6 two 6 1 3 '''

1.3.5 手動指定索引

f1.set_index([pd.Index([1,2,3,4,5,6,7]), 'c']) '''a b dc 1 one 0 7 0 2 one 1 6 1 3 one 2 5 2 4 two 3 4 0 5 two 4 3 1 6 two 5 2 2 7 two 6 1 3'''

1.3.6 索引計(jì)算

s = pd.Series([1,2,3,4,5,6,7]) f1.set_index([s, s**2]) '''a b c d 1 1 0 7 one 0 2 4 1 6 one 1 3 9 2 5 one 2 4 16 3 4 two 0 5 25 4 3 two 1 6 36 5 2 two 2 7 49 6 1 two 3 1 ? '''

1.3.7 修改原始數(shù)據(jù)(inplace)

f1.set_index(['c','d'], inplace=True) f1 '''a b c d one 0 0 71 1 62 2 5 two 0 3 41 4 32 5 23 6 1 '''

2 reset

重新設(shè)置?DataFrame?索引

2.1 使用方法

DataFrame.reset_index(level=None, drop=False, inpalce=False, col_level=0, col_fill=' ')

2.2 參數(shù)說明

level

數(shù)值類型, int、str、tuple或list

默認(rèn)無——?jiǎng)h除所有級別的索引

指定level——?jiǎng)h除指定級別

drop

當(dāng)指定 drop=False 時(shí),則索引列會被還原為普通列;

否則,經(jīng)設(shè)置后的新索引值被會丟棄

默認(rèn)為False

inplace

布爾類型 是否修改原始數(shù)據(jù)框

默認(rèn)False

2.3 舉例

2.3.0 原始數(shù)據(jù)

import pandas as pddata={'a':range(7),'b':range(7,0,-1),'c':['one','one','one','two','two','two','two'],'d':[0,1,2,0,1,2,3]} f1=pd.DataFrame(data) f1=f1.set_index(['c','d']) f1'''a b c d one 0 0 71 1 62 2 5 two 0 3 41 4 32 5 23 6 1 '''

2.3.1 level

level=數(shù)字,或者等于索引的名稱都可以

f1.reset_index(level='c') #等價(jià)于 f1.reset_index(level=0) '''c a b d 0 one 0 7 1 one 1 6 2 one 2 5 0 two 3 4 1 two 4 3 2 two 5 2 3 two 6 1 '''f1.reset_index(level='d') #等價(jià)于 f1.reset_index(level=1) '''d a b c one 0 0 7 one 1 1 6 one 2 2 5 two 0 3 4 two 1 4 3 two 2 5 2 two 3 6 1 '''

總結(jié)

以上是生活随笔為你收集整理的Pandas (GeoPandas)笔记:set_index reset_index的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。