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

Pandas Index Objects

Pandas Index Objects

Pandas' Index object is the base class for identifying axis labels; it provides rich functionality for representing and managing data indexes.

Here are some key features and uses of the Index object:

Pandas provides several different Index types for different scenarios:

Index constructor

Classes/methods Description
pd.Index(data, dtype, name) Create an Index object, supporting custom data, data types, and names.

Index attributes

property Description
Index.values Return the data portion of the Index (a numpy array).
Index.dtype Return the data type of the Index.
Index.name Return or set the name of the Index.
Index.shape Return the shape of the Index (as a tuple).
Index.size Return the number of elements in the Index.
Index.nlevels Return the number of levels in the Index (for a MultiIndex).
Index.is_unique Check whether the values in the Index are unique.
Index.is_monotonic Check whether the Index is monotonically increasing.
Index.is_monotonic_decreasing Check whether the Index is monotonically decreasing.
Index.has_duplicates Check whether the Index has duplicate values.
Index.empty Check whether the Index is empty.

Index methods

Data operations

Method Description
Index.append(other) Append another Index to the current Index.
Index.drop(labels) Delete the specified labels.
Index.insert(loc, item) Insert an element at the specified position.
Index.unique() Return the unique values in the Index.
Index.drop_duplicates() Remove duplicate values.
Index.sort_values() Sort by value.
Index.sort_values(ascending=False) Sort by value in descending order.
Index.tolist() Convert the Index to a list.
Index.to_numpy() Convert the Index to a numpy array.
Index.to_frame() Convert the Index to a DataFrame.
Index.astype(dtype) Convert the Index to a specified data type.
Index.map(func) Apply a function to each element in the Index.
Index.where(cond, other) Replace values based on a condition.
Index.mask(cond, other) Replace values based on a condition (with where no_grad).

Index operations

Method Description
Index.get_loc(key) Return the position of the specified label.
Index.get_indexer(target) Return the position of a target Index within the current Index.
Index.slice_locs(start, end) Return the slice positions for the specified range.
Index.intersection(other) Return the intersection of two Indexes.
Index.union(other) Return the union of two Indexes.
Index.difference(other) Return the difference of two Indexes.
Index.symmetric_difference(other) Return the symmetric difference of two Indexes.
Index.isin(values) Check whether the values in the Index are in the specified list.
Index.reindex(target) Reindex according to a target Index.
Index.reindex_like(other) Reindex according to another Index.

Statistical computation

Method Description
Index.min() Return the minimum value in the Index.
Index.max() Return the maximum value in the Index.
Index.argmin() Return the index position of the minimum value.
Index.argmax() Return the index position of the maximum value.
Index.value_counts() Return the frequency of each value in the Index.

MultiIndex methods

Method Description
pd.MultiIndex.from_arrays() Create a MultiIndex from an array.
pd.MultiIndex.from_tuples() Create a MultiIndex from tuples.
pd.MultiIndex.from_product() Create a MultiIndex from the Cartesian product.
MultiIndex.levels Return the levels of the MultiIndex.
MultiIndex.codes Return the codes of the MultiIndex.
MultiIndex.swaplevel(i, j) Swap the positions of two levels.
MultiIndex.droplevel(level) Delete the specified level.
MultiIndex.set_levels(levels) Set the levels of the MultiIndex.
MultiIndex.set_codes(codes) Set the codes of the MultiIndex.

Example

Example

import

pandas

as

pd

Create an Index

idx

=

pd.

Index

(

[

1

,

2

,

3

]

,

name

=

'MyIndex'

)

View a property

print

(

idx.

values

)

 

Output the data portion

print

(

idx.

name

)

   

Output the name

Data operations

idx_new

=

idx.

append

(

pd.

Index

(

[

4

,

5

]

)

)

print

(

idx_new

)

 

Output the appended Index

Index operations

print

(

idx.

get_loc

(

2

)

)

 

Output the position of label 2

MultiIndex operations

arrays

=

[

[

1

,

1

,

2

,

2

]

,

[

'A'

,

'B'

,

'A'

,

'B'

]

]

multi_idx

=

pd.

MultiIndex

.

from_arrays

(

arrays

,

names

=

(

'Num'

,

'Letter'

)

)

print

(

multi_idx

)

For more detailed information, refer to Pandas official documentation。