Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 971 of 3363
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
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
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
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
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
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
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
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
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
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