首页 > 文章列表 > pandas中index索引功能是什么

pandas中index索引功能是什么

python基础
337 2022-08-07

pandas的索引对象可以用来保存坐标轴标签和其它元数据,是使用过程中必要的参与对象,那pandas中index索引功能是什么呢?pandas中index索引可以轻松的读取数据,更方便的数据查询,使用index查询的时候可以获得性能提升。

一、index索引特点

更方便的数据查询,使用index查询的时候可以获得性能提升;

自动的数据对齐功能;

更多更强大的数据结构支持。

二、index索引用途

1、使用index读取数据

import pandas as pd
df = pd.read_csv("./datas/ml-latest-small/ratings.csv")
df.count()#记录每一列的数目

2、使用index查询数据

# drop==False,让索引列还保持在column
df.set_index("userId", inplace=True, drop=False)
df.head()
df.index
Int64Index([  1,   1,   1,   1,   1,   1,   1,   1,   1,   1,
            ...
            610, 610, 610, 610, 610, 610, 610, 610, 610, 610],
           dtype='int64', name='userId', length=100836)
# 使用index的查询方法
df.loc[500].head(5)

(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)