Martin Reindl
Martin Reindl

Reputation: 1603

How to determine if there are non-string values in a Pandas Series

I am trying to use boolean indexing on a Pandas Series in order to determine if there are values in my Series that are not strings.

My approach so far is:

contains_non_string = s[type(s) != str].any()

When I run this, I get the following error:

TypeError: 'int' object is not subscriptable

How can I correctly determine if there are non-string values in my Series? I am running Python 3.6 and Pandas 0.19.2.

Upvotes: 6

Views: 3130

Answers (3)

user1431480
user1431480

Reputation: 11

Sometimes it's useful to have counts of various types in a Series. The following does exactly the same -

s.apply(type).value_counts()

Upvotes: 1

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

another solution:

In [22]: s = pd.Series([0,'sss',np.nan,3,'aaa',4])

In [23]: s
Out[23]:
0      0
1    sss
2    NaN
3      3
4    aaa
5      4
dtype: object

In [24]: (s.map(type) != str).any()
Out[24]: True

In [25]: s[s.map(type).ne(str)]
Out[25]:
0      0
2    NaN
3      3
5      4
dtype: object

Upvotes: 2

acushner
acushner

Reputation: 9946

you could do something like:

contains_non_string = s[s.apply(type) != str].any()

Upvotes: 4

Related Questions