
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Return a new Index of the values set with the mask
To return a new Index of the values set with the mask, use the index.putmask() method in Pandas. At first, import the required libraries −
import pandas as pd
Creating Pandas index −
index = pd.Index([5, 65, 10, 17, 75, 40])
Display the Pandas index −
print("Pandas Index...\n",index)
Mask and place index values less than 3 with a value 111 −
print("\nMask...\n",index.putmask(index < 30, 111))
Example
Following is the code −
import pandas as pd # Creating Pandas index index = pd.Index([5, 65, 10, 17, 75, 40]) # Display the Pandas index print("Pandas Index...\n",index) # Return the number of elements in the Index print("\nNumber of elements in the index...\n",index.size) # Return the dtype of the data print("\nThe dtype object...\n",index.dtype) # mask and place index values less than 3 with a value 111 print("\nMask...\n",index.putmask(index < 30, 111))
Output
This will produce the following output −
Pandas Index... Int64Index([5, 65, 10, 17, 75, 40], dtype='int64') Number of elements in the index... 6 The dtype object... int64 Mask... Int64Index([111, 65, 111, 111, 75, 40], dtype='int64')
- Related Articles
- Python Pandas - Return a new Index of the values selected by the indices
- Python Pandas - Return a list of the Index values
- Python Pandas - Compute indexer and mask for new index given the current index
- Python Pandas - Return Index with duplicate values removed
- Python Pandas - Return a new Index with elements of index not in other and get the difference
- Python Pandas - Return a new Index with elements of index not in other but unsort the result
- Python Pandas - Return unique values in the index
- Python Pandas - Return the memory usage of the Index values
- Python Pandas - Return Index with duplicate values completely removed
- Python Pandas - Return Index with duplicate values removed except the first occurrence
- Python Pandas - Return Index with duplicate values removed keeping the last occurrence
- Python Pandas - Return Index without NaN values
- Python - Create a new view of the Pandas Index
- Python Pandas - Return the Transpose of the index
- Python Pandas - Return a sorted copy of the index

Advertisements