Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 255 of 2109
Python Pandas CategoricalIndex - Rename categories with dict-like new categories
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 MorePython Pandas CategoricalIndex - Get the categories of this categorical
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 MoreProgram to sort all elements in a given list and merge them into a string in Python
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 MoreProgram to find a path a continuous path in a rectangular area without engaging a bomb in Python
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 MoreProgram to find out is a point is reachable from the current position through given points in Python
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 MoreProgram to find out the maximum points collectable in a game in Python
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 MoreProgram to find out if we win in a game in Python
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 MoreProgram to find out the minimum number of intercountry travels in a road trip in Python
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 MoreProgram to find out the conversion rate of two currencies in Python
Currency conversion is a common problem in financial applications. Given three arrays containing source currencies, destination currencies, and their conversion rates, we need to find the conversion rate between any two currencies using the available exchange paths. The problem provides curr_a (source currencies), curr_b (destination currencies), and conv_rate (conversion rates). We need to find the conversion rate from a source currency to a destination currency, returning 0 if no path exists. Problem Analysis If the input is src = "INR", dest = "JPY", curr_a = ["INR", "GBP", "EUR"], curr_b = ["GBP", "EUR", "JPY"], conv_rate = [0.009, 1.17, ...
Read MoreProgram to find dot product of run length encoded vectors in Python
Suppose we have two lists nums1 and nums2. Each of these two lists represents a vector in run-length encoded form. For example, a vector [1, 1, 1, 2, 2, 2, 2] is represented as [3, 1, 4, 2] (because there are 3 ones and 4 twos). We need to find the dot product of these two vectors, which is the sum of element-wise multiplication of items present in two vectors. So, if the input is like nums1 = [2, 7, 5, 3] and nums2 = [3, 5, 4, 2], then the output will be 109 because: The vectors ...
Read More