Monday 21 August 2023

Pandas: How to Get the Data Type of a Series

Series ‘dtype’ attribute returns the data type of the values in the Series.

 

Example 1: Series holds element of type int64

primes_list = [2, 3, 5, 7, 11]
primes_series = pd.Series(primes_list)

 

Example 2: Series holds element of type bool

boolean_list = [True, False, False, True]
boolean_series = pd.Series(boolean_list)

Example 3: Series holds element of type object

generic_list = ['a', 123, True, 1.34]
generic_series = pd.Series(generic_list)

 

Find the below working application.


 

elements_data_type_in_a_series.py

import pandas as pd

# Series holds element of type int64
primes_list = [2, 3, 5, 7, 11]
primes_series = pd.Series(primes_list)

# Series holds element of type bool
boolean_list = [True, False, False, True]
boolean_series = pd.Series(boolean_list)

# Series holds element of type object
generic_list = ['a', 123, True, 1.34]
generic_series = pd.Series(generic_list)

print('Type of elements in primes_series : ', primes_series.dtype)
print('Type of elements in boolean_series : ', boolean_series.dtype)
print('Type of elements in generic_series : ', generic_series.dtype)

 

Output

Type of elements in primes_series :  int64
Type of elements in boolean_series :  bool
Type of elements in generic_series :  object

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment