Articles on Trending Technologies

Technical articles with clear explanations and examples

Python Pandas - Remove the specified categories from CategoricalIndex

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 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
AmitDiwan
Updated on 26-Mar-2026 263 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
AmitDiwan
Updated on 26-Mar-2026 184 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
AmitDiwan
Updated on 26-Mar-2026 517 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
Arnab Chakraborty
Updated on 26-Mar-2026 331 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
Arnab Chakraborty
Updated on 26-Mar-2026 419 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
Arnab Chakraborty
Updated on 26-Mar-2026 462 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
Arnab Chakraborty
Updated on 26-Mar-2026 327 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

Program to find out if we win in a game in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 712 Views

Suppose we are playing a two-player game where there are n number of marbles and in each round, a player has to take a positive square number of marbles. If a player can't take that square number of marbles, he/she loses. So, given a number n, we have to find out if we can win the game or not. We always make the first turn and select an optimal number of marbles. So, if the input is like 14, then the output will be True. Because at the first turn, we take 9 marbles. That leaves 5 marbles from ...

Read More

Program to find out the minimum number of intercountry travels in a road trip in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 374 Views

In road trip planning, we often need to find the shortest path between cities while minimizing intercountry travels. This problem combines graph traversal with country border crossing penalties to find both the minimum border crossings and total cost. Problem Understanding We have a list of roads R where each element is (x, y, cost) representing a road from city x to city y with given cost. We also have a list C where each element contains cities belonging to the same country. Given a starting city s and destination city e, we need to find the minimum intercountry ...

Read More
Showing 2891–2900 of 61,297 articles
« Prev 1 288 289 290 291 292 6130 Next »
Advertisements