Found 26504 Articles for Server Side Programming

Python - Return index with a specific level removed

AmitDiwan
Updated on 13-Oct-2021 09:08:59

135 Views

To return index with a specific 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 indexmultiIndex = pd.MultiIndex.from_arrays([[5, 10], [15, 20], [25, 30], [35, 40]], names=['a', 'b', 'c', 'd'])Dropping a level. We have passed the position of the level to be removed as a parameter −print("Dropping a level...", multiIndex.droplevel(3)) 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, ... Read More

Python – Return index with a level removed

AmitDiwan
Updated on 13-Oct-2021 09:04:36

109 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

Python Pandas - Set index name for an already created Index object

AmitDiwan
Updated on 13-Oct-2021 09:00:52

268 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

Python Pandas - Return a Series containing counts of unique values from Index object considering NaN values as well

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

183 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

Python Pandas - Return the relative frequency from Index object

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

192 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

Python Pandas - Return a Series containing counts of unique values from Index object sorted in Ascending Order

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

246 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

Python Pandas - Return a Series containing counts of unique values from Index object

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

319 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

Python Pandas - Return number of unique elements in the Index object

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

123 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

Python Pandas - Return unique values in the index

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

Python Pandas - Mask and replace NaNs with a specific value

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

Advertisements