Found 10476 Articles for Python

Python Pandas CategoricalIndex - Check whether the categories have an ordered relationship

AmitDiwan
Updated on 18-Oct-2021 06:44:28

108 Views

To check whether the categories have an ordered relationship, use the ordered property of the CategoricalIndex.At first, import the required libraries −import pandas as pdSet the categories for the categorical using the "categories" parameter. Treat the categorical as ordered using the "ordered" parameter −catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])Display the Categorical Index −print("Categorical Index...", catIndex) Get the categories −print("DisplayingCategories from CategoricalIndex...", catIndex.categories)Check categories for ordered relationship −print("Does categories have ordered relationship...", catIndex.ordered) ExampleFollowing is the code −import pandas as pd # CategoricalIndex can only take on a limited, and usually ... Read More

Python Pandas CategoricalIndex - Get the categories of this categorical

AmitDiwan
Updated on 18-Oct-2021 06:41:04

459 Views

To get the categories of this categorical, use the categories property of the CategoricalIndex in Pandas. At first, import the required libraries −import pandas as pdCategoricalIndex can only take on a limited, and usually fixed, number of possible values (categories. Set the categories for the categorical using the "categories" parameter. Treat the categorical as ordered using the "ordered" parameter −catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])Display the Categorical Index −print("Categorical Index...", catIndex) Get the categories −print("Displaying Categories from CategoricalIndex...", catIndex.categories)ExampleFollowing is the code −import pandas as pd # CategoricalIndex can only ... Read More

Python Pandas CategoricalIndex - Get the category codes of this categorical

AmitDiwan
Updated on 18-Oct-2021 06:38:25

357 Views

To get the category codes of this categorical, use the codes property of the CategoricalIndex in Pandas. At first, import the required libraries −import pandas as pdCategoricalIndex can only take on a limited, and usually fixed, number of possible values (categories). Set the categories for the categorical using the "categories" parameter. Treat the categorical as ordered using the "ordered" parameter. Codes are an array of integers which are the positions of the actual values in the categories array −catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])Display the Categorical Index −print("Categorical Index...", catIndex) Get ... Read More

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

Arnab Chakraborty
Updated on 16-Oct-2021 11:24:52

254 Views

Suppose we are given a list of positive integers. We have to sort the list in descending order and then have to join all the elements in it to form a string. We return the joined string.So, if the input is like input = [415, 78, 954, 123, 5], then the output will be 954785415123To solve this, we will follow these steps −Define a function cmp() . This will take l, rif integer value of (string representation of (l) + string representation of (r)) > integer value of (string representation of (r) + string representation of (l)), thenreturn 1otherwise, return ... Read More

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

Arnab Chakraborty
Updated on 16-Oct-2021 11:20:16

322 Views

Suppose we are given an array mat where the elements are of this form [p, q, r] where p, 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 to ... Read More

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

Arnab Chakraborty
Updated on 16-Oct-2021 11:15:48

352 Views

Suppose in a 2D space a pointer is located at a point p that has coordinates (px, py). Now the pointer has to move to another point q having coordinates (qx, qy). The pointer just cannot move freely, it can travel to q if there are some points located in between. We are given an array of points "paths" that contain various coordinate points. The pointer can move to a point if it is located at (x+1, y) or (x, y+1) or (x-1, y) or (x, y-1) from the current position of the pointer. The given points in the array ... Read More

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

Arnab Chakraborty
Updated on 16-Oct-2021 11:11:56

265 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
Updated on 16-Oct-2021 11:09:01

605 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 which the ... Read More

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

Arnab Chakraborty
Updated on 16-Oct-2021 11:06:24

328 Views

Suppose we have to plan a road trip that involves visiting various cities from different countries. We have a list of roads 'R', where each element is described as (x, y, cost). x signifies the starting city of the road, y signifies the destination city of the road, and cost signifies the cost of traveling by that road. We also have a list 'C' where each element is a country and an element contains the cities of that country. Now, we also have a starting city 's' and a destination city 'e', and we want to travel to the destination ... Read More

Program to find out the conversion rate of two currencies in Python

Arnab Chakraborty
Updated on 16-Oct-2021 11:01:39

259 Views

Suppose we are given three arrays; curr_a, curr_b, and conv_rate. The first array contains some currency names and so does the second one, and the array conv_rate contains the rates of conversion within an item curr_a[i] to cuur_b[i]. The item of conv_rate[i] is the conversion rate between curr_a[i] and curr_b[i]. Now, we are given two currencies src and dest. We have to find out the rate of conversion from src to dest. We return the value as output, and if it is not possible we return 0.So, if the input is like src = "INR", dest = "JPY", curr_a = ... Read More

Advertisements