Pandas (GeoPandas)笔记:set_index reset_index
python包介紹:GeoPandas(初識(shí))_UQI-LIUWJ的博客-CSDN博客
python 庫(kù)整理:pandas_UQI-LIUWJ的博客-CSDN博客
1 set_index
將?DataFrame?中的列轉(zhuǎn)化為行索引。
默認(rèn)情況下,轉(zhuǎn)換之后,原來(lái)的列將不見(jiàn)
1.1 基本使用方法
DataFrame.set_index(keys, drop=True, append=False, inplace=False, verify_integrity=False)1.2 參數(shù)說(shuō)明
| keys | 列標(biāo)簽(需要設(shè)置為索引的列) |
| drop | 刪除用作新索引的列 默認(rèn)為True |
| append | 是否將列附加到現(xiàn)有索引 默認(rèn)為False |
| inplace | 表示當(dāng)前操作是否對(duì)原數(shù)據(jù)生效 默認(rèn)為False |
| verify_integrity | 檢查新索引的副本 將其設(shè)置為False將提高該方法的性能 默認(rèn)為false |
1.3 舉例說(shuō)明
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 手動(dòng)指定索引
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ù)說(shuō)明
| level | 數(shù)值類型, int、str、tuple或list 默認(rèn)無(wú)——?jiǎng)h除所有級(jí)別的索引 指定level——?jiǎng)h除指定級(jí)別 |
| drop | 當(dāng)指定 drop=False 時(shí),則索引列會(huì)被還原為普通列; 否則,經(jīng)設(shè)置后的新索引值被會(huì)丟棄 默認(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的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 文巾解题 面试题 01.06. 字符串压
- 下一篇: osmnx 笔记: plot_graph