0

I am trying to use if statement and str.contains() to generate a label which I can apply to a new column, but I am getting Attribute Error .....

AttributeError: 'str' object has no attribute 'str'

How Can I incorporate str.contains() in the if statement and create a column?

This is a simple if statement I have tried:

def f(row):
    if(row['ContentTitle'] == 'Mr Bean'):
        return 'English Movie'
    if(row['ContentTitle'] == 'Jurassic Park'):
        return 'English'
    if(row['ContentTitle'] == 'Okwonko'):
        return 'Nigeria Movie'
    if(row['ContentTitle'].str.contains('Mama Ntilie')):
        return 'Swahili Movie'
    else:
        return 'Others'

I have tried to use str.contain ('Mama Ntilie') because this is a series and it has multiple seasons and Episode, labeling each one will be tiresome

Streamers_Data_Sample['Content_Watched'] = Streamers_Data_Sample.apply(f, axis = 1)
Bas H
  • 2,114
  • 10
  • 14
  • 23
Mwai.John
  • 25
  • 3
  • .str.contains() will be used for Series. In your case it is a str, you can directly use following code if "Mama Nitle" in row['ContentTitle"]: return "Swahili Movie" – Manoj biroj Jan 31 '23 at 07:50
  • This has worked, Let me try the approach on a large dataset, Thanks in Advance! – Mwai.John Jan 31 '23 at 08:44
  • Thanks @Manojbiroj, The approach has worked for a large data set of more than 10 Mil rows – Mwai.John Jan 31 '23 at 13:07

0 Answers0