Programming Articles

Page 274 of 2547

Python – Return index with a level removed

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 156 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 125 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

Python Pandas - Return a new Index of the values selected by the indices

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 177 Views

To return a new Index of the values selected by the indices, use the index.take() method in Pandas. The take() method allows you to select elements from an Index using their positional indices. Creating a Pandas Index First, let's create a Pandas Index with some sample data − 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) Pandas Index... Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], dtype='object', name='Products') Using take() to Select Values by Index ...

Read More

Python Pandas - Get the seconds from Timedelta object using string input

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

To extract seconds from a Pandas Timedelta object, use the timedelta.seconds property. This property returns the total seconds component of the timedelta duration. Creating a Timedelta Object First, let's create a Timedelta object using string input ? import pandas as pd # Create a Timedelta object with string input timedelta = pd.Timedelta('1 min 30 s') print("Timedelta object:", timedelta) Timedelta object: 0 days 00:01:30 Extracting Seconds Use the seconds property to get the seconds component ? import pandas as pd # Create a Timedelta object timedelta = ...

Read More

Python Pandas - Get the seconds from Timedelta object using integer input

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

To return the seconds from a Timedelta object, use the timedelta.seconds property. The seconds property extracts the seconds component from a Timedelta object when given integer input. Creating a Timedelta Object First, let's create a Timedelta object using integer input with unit 's' for seconds ? import pandas as pd # Create a Timedelta object with 50 seconds timedelta = pd.Timedelta(50, unit='s') # Display the Timedelta print("Timedelta...") print(timedelta) Timedelta... 0 days 00:00:50 Extracting Seconds Value Use the .seconds property to extract the seconds component from the Timedelta object ...

Read More

Python Pandas - Return the seconds from Timedelta object

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

To return the seconds from a Timedelta object, use the timedelta.seconds property. This property extracts only the seconds component from a Timedelta object. Syntax timedelta.seconds Creating a Timedelta Object First, let's create a Timedelta object with various time components ? import pandas as pd # Create a Timedelta object with seconds, milliseconds, and nanoseconds timedelta = pd.Timedelta('10 s 15 ms 33 ns') print("Timedelta...") print(timedelta) Timedelta... 0 days 00:00:10.015000033 Extracting Seconds Use the seconds property to get only the seconds component ? import ...

Read More
Showing 2731–2740 of 25,466 articles
« Prev 1 272 273 274 275 276 2547 Next »
Advertisements