Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Pandas, how to filter a Series based on the type of the values?

Tags:

Given a Series like

import pandas as pd  s = pd.Series(['foo', 'bar', 42]) 

I would like to obtain a 'sub-series' pd.Series(['foo', 'bar']) in which all values are strings. I've tried Boolean indexing like so:

s[isinstance(s, str)] 

but this gives a

KeyError: False

In my search for suitable methods so far I came across select, but this imposes a criterion on the labels, not the values. How can I filter based on (the type of) the values in this case?

like image 547
Kurt Peek Avatar asked Oct 29 '17 10:10

Kurt Peek


People also ask

How do I filter data based on conditions in pandas?

Filter Rows by Condition You can use df[df["Courses"] == 'Spark'] to filter rows by a condition in pandas DataFrame. Not that this expression returns a new DataFrame with selected rows.

Can pandas series have different data types?

Series is a one-dimensional labeled array capable of holding data of the type integer, string, float, python objects, etc. The axis labels are collectively called index.


1 Answers

Use apply or list comprehension:

s[s.apply(lambda x: isinstance(x, str))] 

Same as, thanks Jon Clements♦:

s[s.apply(isinstance, args=(str,))] 

s[[isinstance(x, str) for x in s]] 

All return:

0    foo 1    bar dtype: object 

EDIT:

This is not recommended, thanks cᴏʟᴅsᴘᴇᴇᴅ:

s[s.apply(type) == str] 
like image 62
jezrael Avatar answered Oct 13 '22 17:10

jezrael