How to check if a variable is either a Python list, NumPy array, or pandas series?

Learn, how can we check if a variable is either a Python list, NumPy array, or pandas series?
Submitted by Pranit Sharma, on September 17, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Check whether a given is variable is a Python list, a NumPy array or a Pandas Series

A Python list is a collection of heterogeneous elements and it is mutable in nature. Tuples are also another built-in data type of python used to store heterogeneous elements. Elements inside the list are encapsulated in square brackets whereas elements inside tuples are encapsulated in normal parenthesis. Both list and tuples are considered as a series or collection and we can perform operations like insert, update, and delete with its elements.

Pandas series contains a single list that can store heterogeneous types of data, because of this, the series is also considered a 1-dimensional data structure. When we analyze a series, each value can be considered as a separate row of a single column.

And, NumPy is an array processing package which provides high-performance multidimensional array.

Let us understand with the help of an example,

Python code to check if a variable is either a Python list, NumPy array, or pandas series

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a list
l = [1, 2, 3, 4, 5]

# Creating a numpy array
arr = np.array(l)

# Creating a pandas Series
ser = pd.Series(l)

# Checking type of each item
print("type of L   :", type(l))
print("type of arr :", type(arr))
print("type of Ser :", type(ser))

Output:

type of L   : <class 'list'>
type of arr : <class 'numpy.ndarray'>
type of Ser : <class 'pandas.core.series.Series'>

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.