YouTip LogoYouTip

Pandas Index Object

Pandas Index Object is the base class used to identify axis labels, which provides rich functionality to represent and manage data indexes. Here are some key features and uses of the Index Object: * **Unique Identifier**: The `Index` object provides unique identifiers for data, which is essential for data selection and operations. * **Label-based**: Unlike position-based indexing (such as Python list indexing), `Index` allows label-based indexing, making data operations more intuitive and flexible. * **Data Type Support**: `Index` can hold various types of data, including integers, floats, strings, datetime, etc. Pandas provides several different Index types for different scenarios: * **RangeIndex**: A memory-efficient integer index object, similar to Python's `range` object. * **Index**: The most basic index type, can contain any type of data. * **MultiIndex**: A multi-level index that allows you to have multiple index levels, similar to multiple columns in a DataFrame. * **DatetimeIndex**: An index optimized for datetime data, providing datetime-related functionality. * **PeriodIndex**: An index based on time periods, such as years, quarters, etc. * **TimedeltaIndex**: An index based on time delta (Ξ”t). ### **Index Constructor** | **Class/Method** | **Description** | | --- | --- | | `pd.Index(data, dtype, name)` | Creates an Index object, supports custom data, data type, and name. | * * * ### **Index Attributes** | **Attribute** | **Description** | | --- | --- | | `Index.values` | Returns the data portion of the Index (numpy array). | | `Index.dtype` | Returns the data type of the Index. | | `Index.name` | Returns or sets the name of the Index. | | `Index.shape` | Returns the shape of the Index (as a tuple). | | `Index.size` | Returns the number of elements in the Index. | | `Index.nlevels` | Returns the number of levels in the Index (for MultiIndex). | | `Index.is_unique` | Checks if values in the Index are unique. | | `Index.is_monotonic` | Checks if the Index is monotonically increasing. | | `Index.is_monotonic_decreasing` | Checks if the Index is monotonically decreasing. | | `Index.has_duplicates` | Checks if the Index has duplicate values. | | `Index.empty` | Checks if the Index is empty. | * * * ### **Index Methods** #### **Data Operations** | **Method** | **Description** | | --- | --- | | `Index.append(other)` | Appends another Index to the current Index. | | `Index.drop(labels)` | Deletes the specified labels. | | `Index.insert(loc, item)` | Inserts an element at the specified position. | | `Index.unique()` | Returns unique values in the Index. | | `Index.drop_duplicates()` | Removes duplicate values. | | `Index.sort_values()` | Sorts by values. | | `Index.sort_values(ascending=False)` | Sorts by values in descending order. | | `Index.tolist()` | Converts Index to a list. | | `Index.to_numpy()` | Converts Index to a numpy array. | | `Index.to_frame()` | Converts Index to a DataFrame. | | `Index.astype(dtype)` | Converts Index to the specified data type. | | `Index.map(func)` | Applies a function to each element in the Index. | | `Index.where(cond, other)` | Replaces values based on condition. | | `Index.mask(cond, other)` | Replaces values based on condition (opposite of `where`). | #### **Index Operations** | **Method** | **Description** | | --- | --- | | `Index.get_loc(key)` | Returns the position of the specified label. | | `Index.get_indexer(target)` | Returns the positions of the target Index in the current Index. | | `Index.slice_locs(start, end)` | Returns the slice positions for the specified range. | | `Index.intersection(other)` | Returns the intersection of two Indexes. | | `Index.union(other)` | Returns the union of two Indexes. | | `Index.difference(other)` | Returns the difference of two Indexes. | | `Index.symmetric_difference(other)` | Returns the symmetric difference of two Indexes. | | `Index.isin(values)` | Checks if values in the Index are in the specified list. | | `Index.reindex(target)` | Reindexes according to the target Index. | | `Index.reindex_like(other)` | Reindexes according to another Index. | #### **Statistical Calculations** | **Method** | **Description** | | --- | --- | | `Index.min()` | Returns the minimum value in the Index. | | `Index.max()` | Returns the maximum value in the Index. | | `Index.argmin()` | Returns the index position of the minimum value. | | `Index.argmax()` | Returns the index position of the maximum value. | | `Index.value_counts()` | Returns the frequency of each value in the Index. | #### **MultiIndex Methods** | **Method** | **Description** | | --- | --- | | `pd.MultiIndex.from_arrays()` | Creates a MultiIndex from arrays. | | `pd.MultiIndex.from_tuples()` | Creates a MultiIndex from tuples. | | `pd.MultiIndex.from_product()` | Creates a MultiIndex from Cartesian product. | | `MultiIndex.levels` | Returns the levels of the MultiIndex. | | `MultiIndex.codes` | Returns the codes of the MultiIndex. | | `MultiIndex.swaplevel(i, j)` | Swaps the positions of two levels. | | `MultiIndex.droplevel(level)` | Removes the specified level. | | `MultiIndex.set_levels(levels)` | Sets the levels of the MultiIndex. | | `MultiIndex.set_codes(codes)` | Sets the codes of the MultiIndex. | * * * ### Examples ## Examples import pandas as pd # Create Index idx = pd.Index([1,2,3], name='MyIndex') # View attributes print(idx.values)# Output data portion print(idx.name)# Output name # Data operations idx_new = idx.append(pd.Index([4,5])) print(idx_new)# Output appended Index # Index operations print(idx.get_loc(2))# Output 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, please refer to (https://pandas.pydata.org/docs/reference/indexing.html).
← Pytorch Torch RefPandas General Functions β†’