Python Articles

Page 305 of 855

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 862 Views

To drop rows when any level contains NaN values in a Multi-index, use the dropna() method with the parameter how='any'. This removes all tuples that have at least one NaN value at any level. Creating a Multi-index with NaN Values First, let's create a multi-index containing some NaN values ? import pandas as pd import numpy as np # Create 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]], ...

Read More

Python Pandas - Return Index without NaN values

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 492 Views

To return Index without NaN values, use the index.dropna() method in Pandas. This method creates a new Index object with all NaN values removed, preserving the original data type. Syntax Index.dropna(how='any') Parameters: how − {'any', 'all'}, default 'any'. Determines if row or column is removed from Index, when we have at least one NA or all NA. Creating Index with NaN Values First, let's create a Pandas Index containing some NaN values ? import pandas as pd import numpy as np # Creating Pandas index with some NaN ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 708 Views

To fill NaN values with the specified value in an Index object, use the index.fillna() method in Pandas. This method replaces all NaN values with a specified replacement value and returns a new Index object. Syntax Index.fillna(value=None, downcast=None) Parameters value: The value to replace NaN values with. Can be a scalar value, string, or any valid data type. downcast: Optional parameter to downcast the data type if possible. Creating Index with NaN Values First, let's create a Pandas Index containing some NaN values ? import pandas as pd import ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 184 Views

In Pandas, you can remove multiple levels from a MultiIndex using the droplevel() method. This method accepts level names or positions as parameters and returns a new index with the specified levels removed. Syntax MultiIndex.droplevel(level) Parameters: level − Level name(s) or position(s) to drop. Can be a single value or a list of values. Creating a MultiIndex First, let's create a MultiIndex with named levels ? import pandas as pd # Create a multi-index with named levels multiIndex = pd.MultiIndex.from_arrays( [[5, 10], [15, 20], ...

Read More

Python – Return index with a level removed

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 155 Views

To return an index with a level removed, use the MultiIndex.droplevel() method in Pandas. This method removes one or more levels from a MultiIndex and returns a new index with the remaining levels. Syntax MultiIndex.droplevel(level=0) The level parameter specifies which level to drop. It can be an integer (level position), string (level name), or list of integers/strings for multiple levels. Creating a MultiIndex First, let's create a MultiIndex to demonstrate the droplevel operation ? import pandas as pd # Create a multi-index with 4 levels multiIndex = pd.MultiIndex.from_arrays([ ...

Read More

Python Pandas - Return the relative frequency from Index object

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 256 Views

To return the relative frequency from an Index object, use the index.value_counts() method with the normalize parameter set to True. This calculates the proportion of each unique value relative to the total count. Syntax index.value_counts(normalize=True) Creating a Pandas Index First, let's create a Pandas Index with some duplicate values ‒ import pandas as pd # Creating Pandas index with duplicate values index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) print("Pandas Index...") print(index) print(f"Number of elements: {index.size}") print(f"Data type: {index.dtype}") Pandas Index... Index([50, 10, 70, ...

Read More

Python Pandas - Return unique values in the index

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 4K+ Views

To return unique values in a Pandas index, use the index.unique() method. This method returns unique values in their order of first appearance without sorting them. Syntax index.unique() Creating a Pandas Index First, let's create a Pandas index with duplicate values ? import pandas as pd # Creating Pandas index with duplicates index = pd.Index([10, 50, 70, 10, 90, 50, 10, 30]) print("Original Index:") print(index) Original Index: Int64Index([10, 50, 70, 10, 90, 50, 10, 30], dtype='int64') Getting Unique Values Use the unique() method to extract ...

Read More

Python Pandas - Mask and replace NaNs with a specific value

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

To mask and replace NaNs with a specific value in a Pandas Index, use the putmask() method combined with isna(). This approach allows you to replace all NaN values with a specified replacement value. Syntax index.putmask(condition, value) Parameters: condition: Boolean array or condition to identify elements to replace value: The replacement value for masked elements Creating a Pandas Index with NaNs First, let's create a Pandas Index containing some NaN values ? import pandas as pd import numpy as np # Creating Pandas index with some NaNs index ...

Read More

Python Pandas - Return a new Index of the values set with the mask

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 451 Views

To return a new Index of the values set with the mask, use the index.putmask() method in Pandas. This method creates a new Index where values meeting a specified condition are replaced with a new value. Syntax Index.putmask(mask, value) Parameters The putmask() method accepts the following parameters: mask − A boolean condition that determines which values to replace value − The replacement value for positions where mask is True Example Let's create a Pandas Index and demonstrate how putmask() works ? import pandas as pd # ...

Read More

Python Pandas - Return a new Timedelta ceiled to this resolution

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 122 Views

To return a new Timedelta ceiled to this resolution, use the timedelta.ceil() method. The ceil() method rounds up to the nearest specified frequency unit, similar to the mathematical ceiling function. Syntax timedelta.ceil(freq) Parameters: freq − String representing the frequency to ceil to (e.g., 'D' for days, 'H' for hours, 'T' for minutes) Basic Example Let's create a Timedelta object and ceil it to days frequency ? import pandas as pd # Create a Timedelta object timedelta = pd.Timedelta('6 days 1 min 30 s') # Display the original ...

Read More
Showing 3041–3050 of 8,546 articles
« Prev 1 303 304 305 306 307 855 Next »
Advertisements