Search code examples
pythonstringcomparison

How to compare Series string to another string in python


I'm trying to compare a string from the Series to a string in Python. Here it seems to be ok - I get results of trues and falses:

domains_types['subtype'].astype(str) == 'Default'

for a file: print(domains_types)

But when I try to use it in "if", some problem appears: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

if domains_types['subtype'].astype(str) == 'Default':
    domains_types['Category'] = 'e-commerce'
else:
    domains_types['Category'] = 'other'

I'm new in Python, could you please explain there is the problem here and how to solve it?

I'd like to add a new column with a 'Category" according to the 'sybtype' result I want, here there is only "other" category so far


Solution

  • def cat(val):
        if val == 'Default':
            return('e-commerce')
        else:
            return('other')
    
    df['Category'] = df.apply(lambda x: cat(x['Subtype']), axis=1)
    

    This should return either 'other' or 'e-commerce' dependent on the value of each value in Subtype column. The issue with making an equivalence check against the whole series is explained above.

    Or if you wanted to use a "normal" for loop you can iterate over the dataframe something like:

    newcol = []
    
    for index, row in df.iterrows():
        if row.Subtype == 'Default':
            newcol.append('e-commerce')
        else:
            newcol.append('other')
    
    df['Category'] = newcol