Found 10476 Articles for Python

Python Pandas - Determine if two CategoricalIndex objects contain the same elements

AmitDiwan
Updated on 18-Oct-2021 07:21:39

125 Views

To determine if two CategoricalIndex objects contain the same elements, use the equals() method. 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. Create two CategoricalIndex objects −catIndex1 = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]) catIndex2 = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])Check both the CategoricalIndex objects for equality −print("Check both the CategoricalIndex objects for equality...", catIndex1.equals(catIndex2))ExampleFollowing is the code −import pandas as pd # Set ... Read More

Python Pandas CategoricalIndex - Map values using input correspondence like a dict

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

213 Views

To Map values using input correspondence like a dict, use the CategoricalIndex.map() 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) Map categories −print("CategoricalIndex after mapping...", catIndex.map({'P': 5, 'Q': 10, 'R': 15, 'S': 20}))ExampleFollowing is the code −import pandas as pd # Set the categories for the categorical using the "categories" parameter # Treat the categorical as ordered ... Read More

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

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

171 Views

To set the categories of the CategoricalIndex to be ordered, use the as_ordered() method in Pandas.At first, import the required libraries −import pandas as pdSet the categories for the categorical using the "categories" parameter −catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], categories=["p", "q", "r", "s"])Get the categories −print("Displaying Categories from CategoricalIndex...", catIndex.categories) Set the categories to be ordered −print("CategoricalIndex ordered...", catIndex.as_ordered())ExampleFollowing is the code −import pandas as pd # Set the categories for the categorical using the "categories" parameter catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], categories=["p", "q", "r", "s"]) # Display ... Read More

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

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

185 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

209 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

420 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

186 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

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

Advertisements