If-statement with pandas throws “ValueError: The truth value of a Series is ambiguous”

前端 未结 1 1268
夕颜
夕颜 2021-01-24 18:35

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         


        
相关标签:
1条回答
  • 2021-01-24 19:19

    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, and not.


    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))):
        ...
    
    0 讨论(0)
提交回复
热议问题