Python Pandas CategoricalIndex - Map values using input correspondence like a dict

AmitDiwan
Updated on 26-Mar-2026 16:56:44

301 Views

To map values using input correspondence like a dictionary, use the CategoricalIndex.map() method in Pandas. This method allows you to transform categorical values by mapping them to new values using a dictionary-like object. Creating a CategoricalIndex First, let's create a CategoricalIndex with ordered categories − import pandas as pd # Create CategoricalIndex with ordered categories catIndex = pd.CategoricalIndex(["P", "Q", "R", "S", "P", "Q", "R", "S"], ... Read More

Python Pandas - Set the categories of the CategoricalIndex to be unordered

AmitDiwan
Updated on 26-Mar-2026 16:56:30

263 Views

To set the categories of the CategoricalIndex to be unordered, use the as_unordered() method in Pandas. This method converts an ordered categorical index to an unordered one. Creating an Ordered CategoricalIndex First, let's create an ordered CategoricalIndex using the ordered=True parameter ? import pandas as pd # Create an ordered CategoricalIndex catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, ... Read More

Python Pandas - Remove the specified categories from CategoricalIndex

AmitDiwan
Updated on 26-Mar-2026 16:56:11

3K+ Views

To remove the specified categories from CategoricalIndex, use the remove_categories() method in Pandas. This method removes categories from the index and sets values that were in the removed categories to NaN. Creating a CategoricalIndex First, let's create a CategoricalIndex with some categories ? import pandas as pd # Create CategoricalIndex with categories p, q, r, s cat_index = pd.CategoricalIndex( ["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"] ) print("Original CategoricalIndex:") print(cat_index) print("Categories:") print(cat_index.categories) ... Read More

Python Pandas CategoricalIndex - Add new categories

AmitDiwan
Updated on 26-Mar-2026 16:55:58

276 Views

To add new categories to a Pandas CategoricalIndex, use the add_categories() method. This method extends the available categories without changing the existing data values. Creating a CategoricalIndex First, let's create a CategoricalIndex with initial categories ? import pandas as pd # Create CategoricalIndex with ordered categories catIndex = pd.CategoricalIndex( ["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"] ) print("Original CategoricalIndex:") print(catIndex) Original CategoricalIndex: CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', ... Read More

Python Pandas CategoricalIndex - Rename categories with dict-like new categories

AmitDiwan
Updated on 26-Mar-2026 16:55:39

191 Views

To rename categories with dict-like new categories, use the CategoricalIndex rename_categories() method in Pandas. This method allows you to map old category names to new ones using a dictionary. What is CategoricalIndex? CategoricalIndex can only take on a limited, and usually fixed, number of possible values. It's useful for representing data with a finite set of categories. Creating a CategoricalIndex First, let's create a CategoricalIndex with some sample data ? import pandas as pd # Create CategoricalIndex with ordered categories catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ... Read More

Python Pandas CategoricalIndex - Get the categories of this categorical

AmitDiwan
Updated on 26-Mar-2026 16:55:23

523 Views

To get the categories of a categorical index, use the categories property of the CategoricalIndex in Pandas. A CategoricalIndex can only take on a limited, and usually fixed, number of possible values (categories). Creating a CategoricalIndex First, let's create a CategoricalIndex with specific categories and ordering ? import pandas as pd # Create CategoricalIndex with ordered categories catIndex = pd.CategoricalIndex( ["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"] ) print("Categorical Index...") print(catIndex) ... Read More

Program to sort all elements in a given list and merge them into a string in Python

Arnab Chakraborty
Updated on 26-Mar-2026 16:55:06

343 Views

Suppose we are given a list of positive integers. We have to sort the list in descending order and then join all the elements to form a string. The goal is to arrange numbers so that the resulting concatenated string represents the largest possible number. So, if the input is like input = [415, 78, 954, 123, 5], then the output will be 954785415123. Approach To solve this, we need a custom comparison function that determines which of two numbers should come first when concatenated ? Define a function cmp() that takes two parameters l ... Read More

Program to find a path a continuous path in a rectangular area without engaging a bomb in Python

Arnab Chakraborty
Updated on 26-Mar-2026 16:54:49

434 Views

Suppose we are given an array mat where the elements are of this form [p, q, r] where p and q are geometric coordinates and r is a radius value. The items in the array are the locations of bombs in a rectangular area of a given width w. The rectangle is infinitely long and is bounded by x coordinates x = 0 to x = w. The r value in the bombs position signifies the safety radius of a bomb, meaning anything less than that radius of the bomb will engage it. So, what we have to do is ... Read More

Program to find out is a point is reachable from the current position through given points in Python

Arnab Chakraborty
Updated on 26-Mar-2026 16:54:21

483 Views

In a 2D space, we have a pointer at position (px, py) that needs to move to destination (qx, qy). The pointer can only move to adjacent cells: (x+1, y), (x-1, y), (x, y+1), or (x, y-1). We're given an array of paths containing intermediate points that must be processed serially. We need to find the minimum number of path points required to reach the destination, or return -1 if unreachable. Problem Example If we have px = 1, py = 1, qx = 2, qy = 3, paths = [[1, 2], [0, 1], [0, 2], [1, 3], ... Read More

Program to find out the maximum points collectable in a game in Python

Arnab Chakraborty
Updated on 26-Mar-2026 16:53:57

339 Views

Suppose we are playing a game of cards. We are given several cards arranged linearly with a number on each of them. The numbers on the cards are randomly distributed; and at the beginning and the end of the cards, two cards are inserted with the number 1 on them. Now, in the game, we have to collect the maximum points by picking up the given cards. The cards are represented in an array 'cards' where the elements in the array represent the number of cards[i]. When we pick up card i, we collect points cards[i - 1] * cards[i] ... Read More

Advertisements