Found 10476 Articles for Python

Python Pandas - Drop the value when any level is NaN in a Multi-index

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

795 Views

To drop the value when any level is NaN in a Multi-index, use the multiIndex.dropna() method. Set the parameter how with value any.At first, import the required libraries -import pandas as pd import numpy as npCreate a multi-index with some NaN values. The names parameter sets the names for the levels in the index −multiIndex = pd.MultiIndex.from_arrays([[5, 10], [np.nan, 20], [25, np.nan], [35, 40]], names=['a', 'b', 'c', 'd'])Drop the value when any level is NaN in a Multi-index. Even with a single NaN value, the dropna() will drop all the values. The "how" parameter of the dropna() is used with ... Read More

Python Pandas - Return Index without NaN values

AmitDiwan
Updated on 13-Oct-2021 09:22:41

414 Views

To return Index without NaN values, use the index.dropna() method in Pandas. 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)Drop only the NaN values −print("The Index object after removing NaN values...", index.dropna())ExampleFollowing is the code −import pandas as pd import numpy as np # Creating Pandas index with some NaN values as well index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30]) # Display ... Read More

Python Pandas - Fill NaN values with the specified value in an Index object

AmitDiwan
Updated on 13-Oct-2021 09:20:47

638 Views

To fill NaN values with the specified value in an Index object, use the index.fillna() method in Pandas. 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)Fill the NaN with some specific value −print("Index object after filling NaN value...", index.fillna('Amit')) ExampleFollowing is the code −import pandas as pd import numpy as np # Creating Pandas index with some NaN values as well index = pd.Index([50, 10, 70, np.nan, ... Read More

Python – Remove multiples levels using the level names and return the index

AmitDiwan
Updated on 13-Oct-2021 09:18:27

142 Views

To remove multiples levels using the level names and return the index, use the multiIndex.droplevel(). Set the level names as parameter.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']) Display the multi-index −print("Multi-index...", multiIndex)Dropping multiple levels using the level names. We have passed the names of the levels to be removed as a parameter −print("Dropping multiple level...", multiIndex.droplevel(['a', 'd'])) ExampleFollowing is the code −import pandas as pd # Create a multi-index ... Read More

Python – Remove a level using the name of the level and return the index

AmitDiwan
Updated on 13-Oct-2021 09:15:09

750 Views

To remove a level using the name of the level and return the index, use the multiIndex.droplevel() method in Pandas. Set the name of the level to be removed as parameter.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']) Display the multi-index −print("Multi-index...", multiIndex)Dropping a level using the level name. We have passed the name of the level to be removed as a parameter −print("Dropping a level...", multiIndex.droplevel('b')) ExampleFollowing is the code ... Read More

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

182 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

Advertisements