欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Pandas (GeoPandas)笔记:set_index reset_index

发布时间:2025/4/5 编程问答 61 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Pandas (GeoPandas)笔记:set_index reset_index 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

python包介绍:GeoPandas(初识)_UQI-LIUWJ的博客-CSDN博客

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

1 set_index

将 DataFrame 中的列转化为行索引。

默认情况下,转换之后,原来的列将不见

1.1 基本使用方法

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

1.2 参数说明

keys列标签(需要设置为索引的列)
drop

删除用作新索引的列

默认为True

append

是否将列附加到现有索引

默认为False

inplace

表示当前操作是否对原数据生效

默认为False

verify_integrity

检查新索引的副本

将其设置为False将提高该方法的性能

默认为false

1.3 举例说明

1.3.0 原始数据

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 默认情况

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 索引计算

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 修改原始数据(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

重新设置 DataFrame 索引

2.1 使用方法

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

2.2 参数说明

level

数值类型, int、str、tuple或list

默认无——删除所有级别的索引

指定level——删除指定级别

drop

当指定 drop=False 时,则索引列会被还原为普通列;

否则,经设置后的新索引值被会丢弃

默认为False

inplace

布尔类型 是否修改原始数据框

默认False

2.3 举例

2.3.0 原始数据

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=数字,或者等于索引的名称都可以

f1.reset_index(level='c') #等价于 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') #等价于 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 '''

总结

以上是生活随笔为你收集整理的Pandas (GeoPandas)笔记:set_index reset_index的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。