close

How to check if a variable is either a python list, numpy array or pandas series

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to check if a variable is either a python list, numpy array or pandas series in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to check if a variable is either a python list, numpy array or pandas series?

  1. How to check if a variable is either a python list, numpy array or pandas series?

    You can do it using isinstance:
    import pandas as pd import numpy as np def f(l):

  2. check if a variable is either a python list, numpy array or pandas series

    You can do it using isinstance:
    import pandas as pd import numpy as np def f(l):

Method 1

You can do it using isinstance:

import pandas as pd
import numpy as np
def f(l):
    if isinstance(l,(list,pd.core.series.Series,np.ndarray)):
        print(5)
    else:
        raise Exception('wrong type')

Then f([1,2,3]) prints 5 while f(3.34) raises an error.

Method 2

Python type() should do the job here

l = [1,2]
s= pd.Series(l)
arr = np.array(l) 

When you print

type(l)
list

type(s)
pandas.core.series.Series

type(arr)
numpy.ndarray

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read