Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Pandas - Return the dtype object of the underlying data
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 return the data type of the Index ?
import pandas as pd
# Creating the index
index = pd.Index(['Car', 'Bike', 'Shop', 'Car', 'Airplane', 'Truck'])
# Return the dtype of the data
print("The dtype object:")
print(index.dtype)
# Get additional information about the index
print("\nArray values:")
print(index.values)
print("\nShape of underlying data:")
print(index.shape)
The dtype object: object Array values: ['Car' 'Bike' 'Shop' 'Car' 'Airplane' 'Truck'] Shape of underlying data: (6,)
Different Data Types
Let's examine dtype objects for different data types ?
import pandas as pd
# String Index
str_index = pd.Index(['A', 'B', 'C'])
print("String Index dtype:", str_index.dtype)
# Integer Index
int_index = pd.Index([1, 2, 3])
print("Integer Index dtype:", int_index.dtype)
# Float Index
float_index = pd.Index([1.1, 2.2, 3.3])
print("Float Index dtype:", float_index.dtype)
# Boolean Index
bool_index = pd.Index([True, False, True])
print("Boolean Index dtype:", bool_index.dtype)
String Index dtype: object Integer Index dtype: int64 Float Index dtype: float64 Boolean Index dtype: bool
Conclusion
The index.dtype property returns the data type of elements in a Pandas Index. This is useful for understanding the underlying data structure and ensuring compatibility with operations that require specific data types.
Advertisements
