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
Server Side Programming Articles - Page 852 of 2650
112 Views
To return index with a level removed, use the multiIndex.droplevel() method in Pandas. At first, import the required libraries −import pandas as pdCreate a multi-index. The names parameter sets the names for the levels in the index −multiIndex = pd.MultiIndex.from_arrays([[5, 10], [15, 20], [25, 30], [35, 40]], names=['a', 'b', 'c', 'd']) Dropping a level from the multiindex −print("Dropping a level...", multiIndex.droplevel())ExampleFollowing is the code −import pandas as pd # Create a multi-index # The names parameter sets the names for the levels in the index multiIndex = pd.MultiIndex.from_arrays([[5, 10], [15, 20], [25, 30], [35, 40]], names=['a', 'b', 'c', 'd']) ... Read More
273 Views
To set index name for an already created Index object, use the index.set_names() method in Pandas. At first, import the required libraries −import pandas as pdCreating Pandas index −index = pd.Index(["Electronics", "Mobile Phones", "Accessories", "Home Decor", "Books"]) Display the Pandas index −print("Pandas Index...", index)Set the name of index −print("Set the index name...", index.set_names('Products')) ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index(["Electronics", "Mobile Phones", "Accessories", "Home Decor", "Books"]) # Display the Pandas index print("Pandas Index...", index) # Return the number of elements in the Index print("Number of elements in the index...", ... Read More
190 Views
To return a Series containing counts of unique values from Index object considering NaN values as well with the index.value_counts() method. Set the parameter dropna with value False.At first, import the required libraries -import pandas as pd import numpy as npCreating Pandas index with some NaN values as well −index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30]) Display the Pandas index −print("Pandas Index...", index)Count of unique values using value_counts(). Considering NaN as well using the "False" value of the "dropna" parameter −index.value_counts(dropna=False) ExampleFollowing is the code −import pandas as pd import numpy as np # Creating ... Read More
202 Views
To return the relative frequency from Index object, use the index.value_counts() method with parameter normalize as True.At first, import the required libraries -import pandas as pdCreating Pandas index −index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) Display the Pandas index −print("Pandas Index...", index)Get the count of unique values using value_counts(). Set the parameter "normalize" to True to get the relative frequency −print("Get the relative frequency by dividing all values by the sum of values...", index.value_counts(normalize=True))ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, ... Read More
252 Views
To return a Series containing counts of unique values from Index object sorted in Ascending Order, use the index.value_counts() method with parameter ascending as True.At first, import the required libraries -import pandas as pdCreating Pandas index −index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) Display the Pandas index −print("Pandas Index...", index)Count of unique values sorted in ascending order −print("Get the count of unique values sorted in ascending order..." "", index.value_counts(ascending=True))ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) # Display the ... Read More
328 Views
To return a Series containing counts of unique values from Index object, use the index.value_counts() method in Pandas.At first, import the required libraries -import pandas as pdCreating Pandas index −index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) Display the Pandas index −print("Pandas Index...", index)Count of unique values −print("Get the count of unique values...", index.value_counts()) ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) # Display the Pandas index print("Pandas Index...", index) # Return the number of elements in the Index ... Read More
127 Views
To return number of unique elements in the Index object, use the index.nunique() method in Pandas. At first, import the required libraries −import pandas as pdCreating Pandas index −index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) Display the Pandas index −print("Pandas Index...", index)Get the number of unique values in the index −print("Count of unique values...", index.nunique()) ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) # Display the Pandas index print("Pandas Index...", index) # Return the number of elements in ... Read More
4K+ Views
To return unique values in the index, use the index.unique() method in Pandas. At first, import the required libraries -import pandas as pdCreating Pandas index −index = pd.Index([10, 50, 70, 10, 90, 50, 10, 30]) Display the Pandas index −print("Pandas Index...", index)Get the unique values from the index. Unique values are returned in order of appearance, this does NOT sort −index.unique() ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index([10, 50, 70, 10, 90, 50, 10, 30]) # Display the Pandas index print("Pandas Index...", index) # Return the number of elements ... Read More
1K+ Views
To mask and replace NaNs with a specific value, use the index.putmask() method. Within that, set the index.isna() method.At first, import the required libraries -import pandas as pd import numpy as npCreating Pandas index with some NaNs −index = pd.Index([5, 65, 10, np.nan, 75, np.nan]) Display the Pandas index −print("Pandas Index...", index)Mask and replace NaN index values with a specific value −print("Mask...", index.putmask(index.isna(), 111)) ExampleFollowing is the code −import pandas as pd import numpy as np # Creating Pandas index with some NaNs index = pd.Index([5, 65, 10, np.nan, 75, np.nan]) # Display the Pandas index print("Pandas Index...", ... Read More
403 Views
To return a new Index of the values set with the mask, use the index.putmask() method in Pandas. At first, import the required libraries −import pandas as pdCreating Pandas index −index = pd.Index([5, 65, 10, 17, 75, 40]) Display the Pandas index −print("Pandas Index...", index)Mask and place index values less than 3 with a value 111 −print("Mask...", index.putmask(index < 30, 111)) ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index([5, 65, 10, 17, 75, 40]) # Display the Pandas index print("Pandas Index...", index) # Return the number of elements in the ... Read More