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?
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.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With