Count Unique Values from Index Object in Pandas

AmitDiwan
Updated on 13-Oct-2021 08:58:55

191 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

Return Relative Frequency from Index Object in Python Pandas

AmitDiwan
Updated on 13-Oct-2021 08:56:37

203 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

Count Unique Values from Index in Pandas

AmitDiwan
Updated on 13-Oct-2021 08:53:42

254 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

Count Unique Values from Index Object in Python Pandas

AmitDiwan
Updated on 13-Oct-2021 08:51:00

331 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

Return Number of Unique Elements in the Index Object using Python Pandas

AmitDiwan
Updated on 13-Oct-2021 08:48:01

129 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

Return Unique Values in the Index using Python Pandas

AmitDiwan
Updated on 13-Oct-2021 08:45:34

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

Mask and Replace NaNs with a Specific Value in Pandas

AmitDiwan
Updated on 13-Oct-2021 08:12:26

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

Return New Index of Values Set with Mask in Python Pandas

AmitDiwan
Updated on 13-Oct-2021 08:10:32

405 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

Return New Timedelta Ceiled to Resolution in Python Pandas

Arnab Chakraborty
Updated on 13-Oct-2021 08:09:21

87 Views

To return a new Timedelta ceiled to this resolution, use the timedelta.ceil() method. With that, set the resolution using the freq parameter.At first, import the required libraries −import pandas as pdTimeDeltas is Python’s standard datetime library uses a different representation timedelta’s. Create a Timedelta objecttimedelta = pd.Timedelta('6 days 1 min 30 s') Display the Timedeltaprint("Timedelta...", timedelta)Return the ceiled Timestamp ceiled to days frequencyres = timedelta.ceil(freq='D') ExampleFollowing is the code import pandas as pd # TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s # create a Timedelta object timedelta = pd.Timedelta('6 days 1 min 30 s') # ... Read More

Return New Index of Selected Values in Pandas

AmitDiwan
Updated on 13-Oct-2021 08:07:57

138 Views

To return a new Index of the values selected by the indices, use the index.take() method in Pandas. At first, import the required libraries -import pandas as pdCreating Pandas index −index = pd.Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], name ='Products') Display the Pandas index −print("Pandas Index...", index)Getting a new index of the values selected by indices −print("A new Index of the values selected by the indices...", index.take([1, 2]))ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], name ='Products') # Display the Pandas index print("Pandas Index...", index) # Return ... Read More

Advertisements