Found 26504 Articles for Server Side Programming

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

AmitDiwan
Updated on 18-Oct-2021 06:50:12

133 Views

To rename categories with dict-like new categories, use the CategoricalIndex rename_categories() method 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. 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 CategoricalIndex −print("CategoricalIndex...", catIndex)Rename categories. Set the new dict-like categories that will replace the old categoriesprint("CategoricalIndex after renaming categories...", catIndex.rename_categories({'p': 5, 'q': 10, 'r': 15, 's': 20})) ExampleFollowing is the code −import pandas as pd # CategoricalIndex can ... Read More

Python Pandas CategoricalIndex - Rename categories

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

294 Views

To rename categories, use the CategoricalIndex rename_categories() method 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. 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"])Rename categories. Set the new categories that will replace the old categories −print("CategoricalIndex after renaming categories...", catIndex.rename_categories([5, 10, 15, 20])) ExampleFollowing is the code −import pandas as pd # CategoricalIndex can only take ... Read More

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 find out the number of submatrices from a matrix where the sum of elements is equal to a specific value in C++

Arnab Chakraborty
Updated on 16-Oct-2021 12:08:35

277 Views

Suppose we are given a matrix that contains integer values. We have to find out the submatrices from the matrix where the sum of elements of the matrices is equal to a given target sum value. We return the number of submatrices.So, if the input is like0010010001011101and target = 5, then the output will be 3.The number of submatrices whose sum of elements is equal to 6 is 2.To solve this, we will follow these steps −n := size of matm := (if n is same as 0, then 0, otherwise size of mat[0])if m > n, then −Define one ... Read More

Program to find out the number of non-zero submatrices in C++

Arnab Chakraborty
Updated on 16-Oct-2021 12:02:14

469 Views

Suppose we are given a matrix that contains only two values; 1s and 0s. We have to find out the number of submatrices in the given matrix that contains all 1s. We print the value as output.So, if the input is like0010010001011101then the output will be 12.To solve this, we will follow these steps −n := size of matrixm := size of matrix[0]Define an array add of size: n+1 x m+1.for initialize i := 0, when i < n, update (increase i by 1), do −for initialize j := 0, when j < m, update (increase j by 1), do ... Read More

Program to find minimum cost to connect each Cartesian coordinates in C++

Arnab Chakraborty
Updated on 16-Oct-2021 11:53:51

260 Views

Suppose we have a list of 2D Cartesian coordinate points (x, y). We can connect (x0, y0) and (x1, y1), whose cost is |x0 - x1| + |y0 - y1|. If we are allowed to connect any number of points, we have to find the minimum cost necessary such that every point is connected by a path.So, if the input is like points = [[0, 0], [0, 2], [0, -2], [2, 0], [-2, 0], [2, 3], [2, -3]], then the output will be 14 because, from (0, 0) to (0, 2), (0, -2), (2, 0), (-2, 0), costs are 2, ... 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

324 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

Advertisements