Yari
Yari

Reputation: 907

Check for a character in a list of strings

Python's in doesn't work on whole columns:

> dfTrain['name'][22]
  'McGowan, Miss. Anna "Annie"'

> "\"" in dfTrain['name'][22]
  True

> "\"" in dfTrain['name']
False

How can I check if a character is present in a list of strings?

Upvotes: 5

Views: 4440

Answers (3)

CoderBC
CoderBC

Reputation: 1392

There can be several ways of doing this:

1) One of the things you can do is

"\"" in dfTrain['name'].to_string()

This returns True if any of the names in the df contain a ".

2) The other way could be not dfTrain[dfTrain['name'].str.contains('"')].empty

This is because, I am finding all columns that contain ". If there are no columns that contain " it means the dataframe returned will be empty. If the dataframe returned is empty(True) then none of the columns contain a " for which you want the output 'False' (hence the 'not' statement')

Upvotes: 1

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210882

you can join() elements in each row into one string and use contains() in order to check whether it contains ":

In [11]: df
Out[11]:
                                      name
0                           [test1, test2]
1                           [another test]
2                      [yet, another test]
3  [McGowan, Miss. Anna "Annie", aaa, bbb]

In [12]: df['name'].str.join('').str.contains('"')
Out[12]:
0    False
1    False
2    False
3     True
Name: name, dtype: bool

Upvotes: 0

abhinsit
abhinsit

Reputation: 3272

"\"" in dfTrain['name'][22] is 'McGowan, Miss. Anna "Annie"' which contains "\"

while dfTrain['name'] is a list and you dont have a "\" as element in list

Similar example as yours:

>>> nested_list_example = ["abhishek","ralesh","wr'"]
>>> "wr'" in nested_list_example
True
>>> "'" in nested_list_example
False
>>> "'" in nested_list_example[2]
True

Upvotes: 4

Related Questions