I am wanting to see if a particular string is present in my dataframe for one column and fire off an API if it is. code so far:
if new_df.col1.str.contains(\'str
The reason for this error is because if-else
expressions in python are meant to compare scalar boolean values. You passed a Series.
See more in docs section on Pandas Gotchas.
pandas follows the NumPy convention of raising an error when you try to convert something to a
bool
. This happens in an if-statement or when using the boolean operations:and
,or
, andnot
.
In this example, you can combine them into a single regex pattern 'string2?'
which indicates that '2'
is optional.
def make_request():
...
for mask in new_df.col1.str.contains(r'string2?'):
if mask:
make_request()
If your make_request
function returns something, you can call it in a list comp and assign back:
df['response'] = [
make_request() if m else np.nan for m in new_df.col1.str.contains(r'string2?')]
Another option is using regex OR pipe to join strings in a list.
import re
words = ['string', 'string2']
for mask in new_df.col1.str.contains('|'.join(map(re.escape, words))):
...