Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pandas Index (Indexing)

Pandas Index (Indexing)

Index is a core component of Pandas data structures; it determines how data is organized and accessed. Pandas supports many types of indexes, from the simple RangeIndex to the complex multi-level index (MultiIndex). This section introduces how to use each index type in detail.


Basic concepts of Index

An Index is similar to a database primary key or an Excel row number, used to uniquely identify each row of data. A DataFrame has two parts: the row index (index) and the column index (columns).

Example

import

pandas

as

pd

Create a simple DataFrame (using RangeIndex by default)

df

=

pd.

DataFrame

(

{

   

"Name"

:

[

"Zhang San"

,

"Li Si"

,

"Wang Wu"

]

,

   

"Age"

:

[

25

,

30

,

28

]

,

   

"City"

:

[

"Beijing"

,

"Shanghai"

,

"Guangzhou"

]

}

)

print

(

"DataFrame info:"

)

print

(

f

"Row index: {df.index.tolist()}"

)

print

(

f

"Column index: {df.columns.tolist()}"

)

print

(

"\nData:"

)

print

(

df

)


RangeIndex

RangeIndex is the default integer index, similar to Python's range(n), starting from 0 and increasing.

Creation and usage

Example

import

pandas

as

pd

Create a RangeIndex

idx

=

pd.

RangeIndex

(

start

=

0

,

stop

=

10

,

step

=

1

)

print

(

f

"RangeIndex: {idx}"

)

print

(

f

"Type: {type(idx)}"

)

DataFrame uses RangeIndex by default

df

=

pd.

DataFrame

(

{

"A"

:

[

1

,

2

,

3

]

}

,

index

=

range

(

3

)

)

print

(

f

"\nDefault index type: {type(df.index)}"

)

Resetting the index also returns a RangeIndex

df_reset

=

df.

reset_index

(

)

print

(

f

"Index type after reset: {type(df_reset.index)}"

)

RangeIndex features


Index type conversion

Index supports conversion between many types.

Convert to other index types

Example

import

pandas

as

pd

Create a sample DataFrame

df

=

pd.

DataFrame

(

{

"Value"

:

[

1

,

2

,

3

,

4

]

}

,

index

=

[

10

,

20

,

30

,

40

]

)

Convert to an Index type

print

(

"Original index:"

,

df.

index

)

print

(

"Index type:"

,

type

(

df.

index

)

)

Convert to a list

idx_list

=

df.

index

.

tolist

(

)

print

(

f

"Converted to list: {idx_list}"

)

Convert to a NumPy array

idx_array

=

df.

index

.

values

print

(

f

"Converted to array: {idx_array}"

)

Reset the index to RangeIndex

df

=

df.

reset_index

(

drop

=

True

)

print

(

f

"Reset to RangeIndex: {df.index}"

)


Set a custom index

You can use a column of the DataFrame as the row index.

Using set_index

Example

import

pandas

as

pd

Create DataFrame

df

=

pd.

DataFrame

(

{

   

"Student ID"

:

[

"S001"

,

"S002"

,

"S003"

,

"S004"

]

,

   

"Name"

:

[

"Zhang San"

,

"Li Si"

,

"Wang Wu"

,

"Zhao Liu"

]

,

   

"Score"

:

[

85

,

92

,

78

,

90

]

}

)

print

(

"Original data:"

)

print

(

df

)

print

(

)

Set the "student ID" column as the index

df1

=

df.

set_index

(

"Student ID"

)

print

(

"Set student ID as index:"

)

print

(

df1

)

print

(

)

Set multiple indexes (creates a MultiIndex)

df2

=

df.

set_index

(

[

"Student ID"

,

"Name"

]

)

print

(

"Set multiple indexes:"

)

print

(

df2

)

Create using the index parameter

Example

import

pandas

as

pd

Specify the index directly when creating the DataFrame

df

=

pd.

DataFrame

(

   

{

"Name"

:

[

"Zhang San"

,

"Li Si"

,

"Wang Wu"

]

,

"Age"

:

[

25

,

30

,

28

]

}

,

    index

=

[

"A001"

,

"A002"

,

"A003"

]

)

print

(

df

)

print

(

f

"\nIndex: {df.index.tolist()}"

)

Using DatetimeIndex

dates

=

pd.

date_range

(

"2024-01-01"

,

periods

=

3

,

freq

=

"D"

)

df_date

=

pd.

DataFrame

(

{

"Value"

:

[

100

,

200

,

300

]

}

,

index

=

dates

)

print

(

"\nUsing a date index:"

)

print

(

df_date

)

print

(

f

"Index type: {type(df_date.index)}"

)


Index operations

Reset the index

Example

import

pandas

as

pd

Create a DataFrame with a custom index

df

=

pd.

DataFrame

(

   

{

"Name"

:

[

"Zhang San"

,

"Li Si"

]

,

"Score"

:

[

85

,

92

]

}

,

    index

=

[

"A001"

,

"A002"

]

)

Reset the index to the default RangeIndex

df_reset

=

df.

reset_index

(

)

print

(

"Reset index:"

)

print

(

df_reset

)

drop parameter: whether to discard the original index column

df_reset2

=

df.

reset_index

(

drop

=

True

)

print

(

"\nReset and discard the original index:"

)

print

(

df_reset2

)

Reindexing

Example

import

pandas

as

pd

Create DataFrame

df

=

pd.

DataFrame

(

{

"A"

:

[

1

,

2

,

3

]

,

"B"

:

[

4

,

5

,

6

]

}

,

index

=

[

1

,

2

,

3

]

)

Reindexing (changing the index order)

df_reindex

=

df.

reindex

(

[

1

,

2

,

3

,

4

,

5

]

)

print

(

"Reindexing (filling missing values with NaN):"

)

print

(

df_reindex

)

Fill missing values using fill_value

df_reindex2

=

df.

reindex

(

[

1

,

2

,

3

,

4

,

5

]

,

fill_value

=

0

)

print

(

"\nReindexing (filled with 0):"

)

print

(

df_reindex2

)

Index level operations

Example

import

pandas

as

pd

Create a multi-level index DataFrame

df

=

pd.

DataFrame

(

{

   

"Chinese"

:

[

85

,

92

,

78

]

,

   

"Math"

:

[

90

,

88

,

95

]

}

,

index

=

pd.

MultiIndex

.

from_tuples

(

   

[

(

"Grade 10"

,

"Class A"

)

,

(

"Grade 10"

,

"Class B"

)

,

(

"Grade 11"

,

"Class A"

)

]

,

    names

=

[

"Grade"

,

"Class"

]

)

)

print

(

"Multi-level index DataFrame:"

)

print

(

df

)

print

(

)

Get the outer index

print

(

f

"Outer index (grade): {df.index.get_level_values(0).tolist()}"

)

print

(

f

"Inner index (class): {df.index.get_level_values(1).tolist()}"

)


Index attributes and methods

Attribute / method Description Example
index.tolist() Convert to a Python list df.index.tolist()
index.values Convert to a NumPy array df.index.values
index.unique() Get unique values df.index.unique()
index.is_unique Check whether the index is unique df.index.is_unique
index.astype() Convert the index type df.index.astype(str)

Example of using index attributes

Example

import

pandas

as

pd

Create a sample DataFrame

df

=

pd.

DataFrame

(

   

{

"Value"

:

[

1

,

2

,

3

,

4

]

}

,

    index

=

[

"a"

,

"b"

,

"c"

,

"d"

]

)

View index attributes

print

(

f

"Is index unique: {df.index.is_unique}"

)

print

(

f

"Index length: {len(df.index)}"

)

print

(

f

"Index data type: {df.index.dtype}"

)

Convert to string type

str_index

=

df.

index

.

astype

(

str

)

print

(

f

"Type after conversion: {str_index.dtype}"

)


Practical: using indexes to improve query efficiency

In real data analysis, setting indexes appropriately can significantly improve query efficiency.

Example

import

pandas

as

pd

Simulate business data

df

=

pd.

DataFrame

(

{

   

"order_id"

:

range

(

1000

)

,

   

"Customer ID"

:

[

f

"C{i%100:03d}"

for

i

in

range

(

1000

)

]

,

   

"Product"

:

[

f

"Product {i%20}"

for

i

in

range

(

1000

)

]

,

   

"amount"

:

[

round

(

i *

1.5

,

2

)

for

i

in

range

(

1000

)

]

}

)

Set frequently queried columns as the index

df_indexed

=

df.

set_index

(

[

"Customer ID"

,

"Product"

]

)

Fast lookup using an index (similar to a database primary key query)

result

=

df_indexed.

loc

[

(

"C001"

,

"Product 1"

)

]

print

(

"Query a single customer's products using the index:"

)

print

(

result

)

Group statistics by customer ID

print

(

"\nTotal spending per customer:"

)

customer_total

=

df.

groupby

(

"Customer ID"

)

[

"amount"

]

.

sum

(

)

print

(

customer_total.

head

(

10

)

)


Notes

1. Index values must be unique

If index values are not unique, some operations (such as loc lookup) will return multiple matching rows.

2. Indexes can have names

Naming an index can improve code readability:df.index.name = "学号"

3. Indexes follow data operations

Operations such as slicing and filtering preserve the index, so you need to pay attention to the correspondence between the index and the data.

Indexes are the key to Pandas performance. Setting frequently queried columns as indexes can greatly speed up lookups, but too many indexes increase write overhead, so you need to weigh the trade-offs.