Programming Articles - Page 971 of 3363

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

AmitDiwan
Updated on 18-Oct-2021 07:13:15

222 Views

To set the categories of the CategoricalIndex to be unordered, use the as_unordered() method in Pandas.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 with value True −catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])Set the categories to be unordered −print("CategoricalIndex unordered...", catIndex.as_unordered()) ExampleFollowing is the code −import pandas as pd # Set the categories for the categorical using the "categories" parameter # Treat the categorical as ordered using the "ordered" parameter with ... Read More

Python Pandas - Remove the specified categories from CategoricalIndex

AmitDiwan
Updated on 18-Oct-2021 07:10:02

3K+ Views

To remove the specified categories from CategoricalIndex, use the remove_categories() method in Pandas.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"])Remove categories using remove_categories(). Set the categories to be removed as a parameter. Values which were in the removed categories will be set to NaN −print("CategoricalIndex after removing specified categories...", catIndex.remove_categories(["p", "q"]))ExampleFollowing is the code −import pandas as pd # Set the categories for the ... Read More

Python Pandas CategoricalIndex - Add new categories

AmitDiwan
Updated on 18-Oct-2021 07:07:25

237 Views

To add new categories, use the CategoricalIndex add_categories() method in Pandas. 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 CategoricalIndex −print("CategoricalIndex...", catIndex) Add new categories using add_categories(). Set the new categories as a parameter. The new categories will be included at the last/highest place in the categories −print("CategoricalIndex after adding new categories...", catIndex.add_categories(["a", "b", "c", "d"]))ExampleFollowing is the code −import pandas as pd ... Read More

Python Pandas CategoricalIndex - Reorder categories

AmitDiwan
Updated on 18-Oct-2021 07:04:54

440 Views

To eorder categories, use the CategoricalIndex reorder_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"])Display the CategoricalIndex −print("CategoricalIndex...", catIndex) Get the categories −print("DisplayingCategories from CategoricalIndex...", catIndex.categories)Reorder categories using reorder_categories(). Set the categories in new order as a parameter −print("CategoricalIndex after reordering categories...", catIndex.reorder_categories(["r", "s", "q", "p"]))ExampleFollowing is the ... Read More

Python Pandas CategoricalIndex - Rename categories with lambda

AmitDiwan
Updated on 18-Oct-2021 07:02:14

205 Views

To rename categories with Lambda, 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"]) Display the CategoricalIndex −print("CategoricalIndex...", catIndex)Rename categories using rename_categories(). Set the new categories that with use lambda and set lowercase for all the categories −print("CategoricalIndex after renaming categories...", catIndex.rename_categories(lambda a: a.lower())) ExampleFollowing is ... Read More

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

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

153 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

309 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

125 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

484 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

381 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

Advertisements