Found 26504 Articles for Server Side Programming

Python Pandas - Check whether the IntervalIndex intervals are closed on the left-side, right-side, both or neither

AmitDiwan
Updated on 18-Oct-2021 07:27:19

151 Views

To check whether the IntervalIndex intervals are closed on the left-side, right-side, both or neither, use the interval.closed property.At first, import the required libraries −import pandas as pdCreate IntervalIndex −interval = pd.IntervalIndex.from_arrays([5, 10, 15], [15, 20, 25]) Display the interval −print("IntervalIndex...", interval)Check whether the IntervalIndex is closed on the left-side, right-side, both or neither −print("Checking for the type of IntervalIndex...", interval.closed) ExampleFollowing is the code −import pandas as pd # Create IntervalIndex interval = pd.IntervalIndex.from_arrays([5, 10, 15], [15, 20, 25]) # Display the interval print("IntervalIndex...", interval) # Display the interval length print("IntervalIndex length...", interval.length) # Check ... Read More

Python Pandas - Create an IntervalIndex

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

569 Views

To create an IntervalIndex in Pandas, use the pandas.IntervalIndex.from_arrays() method. At first, import the required libraries −import pandas as pdCreate IntervalIndex −interval = pd.IntervalIndex.from_arrays([5, 10, 15], [10, 15, 20]) Display the interval −print("IntervalIndex...",interval)ExampleFollowing is the code −import pandas as pd # Create IntervalIndex interval = pd.IntervalIndex.from_arrays([5, 10, 15], [10, 15, 20]) # display the interval print("IntervalIndex...",interval) # display the interval length print("IntervalIndex length...",interval.length)OutputThis will produce the following output −IntervalIndex... IntervalIndex([(5, 10], (10, 15], (15, 20]], dtype='interval[int64, right]') IntervalIndex length... Int64Index([5, 5, 5], dtype='int64')

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

186 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

210 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

Advertisements