-3

I'm programming a loop in which in each iteration the variable is either a series or an integer. I need to do different things in each case. How do I check the datatype and use it in a condition?

I've tried doing if(type(i)==) But it does not work

KiraDeus
  • 39
  • 1
  • 6
  • 1
    Possible duplicate of [What's the canonical way to check for type in python?](https://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python) – skrx Jul 26 '17 at 12:51

2 Answers2

4

I think you need compare with pd.Series:

i = pd.Series([1,2])
print (type(i) == pd.Series)
True

i = 5
print (type(i) == int)
True
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
3

You can use the isinstance(object, classinfo) built-in function from Python defined here.

Return true if the object argument is an instance of the classinfo argument

So you can use it like this:

if isinstance(i, pd.Series)

and

if isinstance(i, int)
Marv
  • 165
  • 8