Pandas Articles

Page 13 of 42

Python Pandas - Return a tuple of the shape of the underlying data

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 591 Views

To return a tuple of the shape of the underlying data, use the index.shape property in Pandas. The shape property returns the dimensions of the Index as a tuple. Syntax index.shape This property returns a tuple where the first element represents the number of elements in the Index. Creating a Basic Index Let's start by creating a Pandas Index and examining its shape ? import pandas as pd # Creating the index index = pd.Index(['Car', 'Bike', 'Truck', 'Car', 'Airplane']) # Display the index print("Pandas Index...") print(index) # Return ...

Read More

Python Pandas - Return a string of the type inferred from the values

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 169 Views

To return a string of the type inferred from the values, use the index.inferred_type property in Pandas. This property analyzes the data and returns a string indicating the inferred data type. Syntax index.inferred_type Basic Example Let's create an index with mixed data types and see how Pandas infers the type ? import pandas as pd import numpy as np # Creating an index with mixed string and NaN values index = pd.Index(['Car', 'Bike', np.nan, 'Car', np.nan, 'Ship', None, None]) # Display the index print("Pandas Index...") print(index) # Return a ...

Read More

Python Pandas - Return the dtype object of the underlying data

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 486 Views

To return the dtype object of the underlying data, use the index.dtype property in Pandas. The dtype represents the data type of elements stored in the Index. Syntax index.dtype Creating a Pandas Index First, let's create a Pandas Index with string values − import pandas as pd # Creating the index index = pd.Index(['Car', 'Bike', 'Shop', 'Car', 'Airplane', 'Truck']) # Display the index print("Pandas Index...") print(index) Pandas Index... Index(['Car', 'Bike', 'Shop', 'Car', 'Airplane', 'Truck'], dtype='object') Getting the Dtype Object Use the dtype property to ...

Read More

Python Pandas - Check if the index has NaNs

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

To check if a Pandas index contains NaN values, use the hasnans property. This boolean property returns True if any NaN values are present in the index. Syntax index.hasnans Creating an Index with NaN Values First, let's create an index that contains some NaN values ? import pandas as pd import numpy as np # Creating an index with NaN values index = pd.Index(['Car', 'Bike', np.nan, 'Car', np.nan, 'Ship']) print("Pandas Index...") print(index) Pandas Index... Index(['Car', 'Bike', nan, 'Car', nan, 'Ship'], dtype='object') Checking for NaN Values ...

Read More

Python Pandas - Check if the index has duplicate values

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

To check if the index has duplicate values, use the has_duplicates property in Pandas. This property returns True if any values appear more than once in the index, and False otherwise. Syntax index.has_duplicates Creating an Index with Duplicates Let's create an index with duplicate values and check for duplicates ? import pandas as pd # Creating the index with duplicates index = pd.Index(['Car', 'Bike', 'Truck', 'Car', 'Airplane']) # Display the index print("Pandas Index...") print(index) # Check if the index has duplicate values print("Has duplicate values?") print(index.has_duplicates) ...

Read More

Python Pandas - Check if the index has unique values

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

In pandas, you can check if an index contains unique values using the is_unique property. This property returns True if all index values are unique, and False if there are duplicates. Syntax index.is_unique Example with Unique Values Let's create an index with unique values and check if it has unique values ? import pandas as pd # Creating an index with unique values index = pd.Index([50, 40, 30, 20, 10]) # Display the index print("Pandas Index...") print(index) # Check if the index has unique values print("Is the Pandas index ...

Read More

Python Pandas - Return the Transpose of the index

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 200 Views

In Pandas, the index.T property returns the transpose of an index. For a one-dimensional index, the transpose is the index itself since there's only one dimension. Syntax index.T Creating a Pandas Index Let's start by creating a simple Pandas Index ? import pandas as pd # Creating the index index = pd.Index(['Car', 'Bike', 'Truck', 'Ship', 'Airplane']) # Display the index print("Pandas Index:") print(index) Pandas Index: Index(['Car', 'Bike', 'Truck', 'Ship', 'Airplane'], dtype='object') Using the T Property Now let's use the T property to get the ...

Read More

Python Pandas - Check elementwise if the Intervals contain the value

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 682 Views

To check elementwise if the Intervals contain a specific value, use the contains() method on a Pandas IntervalArray. This method returns a boolean array indicating which intervals contain the given value. Creating an IntervalArray First, let's create an IntervalArray from break points ? import pandas as pd # Create IntervalArray from break points array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5]) print("Our IntervalArray:") print(array) Our IntervalArray: [(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]] Length: 5, dtype: interval[int64, right] Using contains() Method The contains() method checks ...

Read More

Python Pandas - Construct an IntervalArray from an array of splits

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 139 Views

To construct an IntervalArray from an array of splits, use the pandas.arrays.IntervalArray.from_breaks() method. This creates intervals from breakpoints where each consecutive pair of values forms an interval. Syntax pandas.arrays.IntervalArray.from_breaks(breaks, closed='right', copy=False, dtype=None) Creating IntervalArray from Breaks First, import the required library ? import pandas as pd # Construct a new IntervalArray from an array-like of splits array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5]) # Display the IntervalArray print("Our IntervalArray...", array) Our IntervalArray... [(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]] Length: 5, dtype: ...

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
Showing 121–130 of 418 articles
« Prev 1 11 12 13 14 15 42 Next »
Advertisements