-2

I have a problem with my function and I keep getting the attribute error pasted in the title.

My Code

def conversion(x):
    duration = x[0]

    if duration.str.contains('min'):
        return duration.split()[0]
    elif duration.str.,contains('Season'):
        return duration.split()[0]
    else:
        return duration

df['split'] = df[['duration']].apply(conversion)

The data frame contains a column with the duration of Netflix episodes written as a string. Within the column there is a mix of values in two structures, '1 Season' and '94 mins' as an example.

My function was supposed to read in the value check the string structure and return only the number. Thank you.

2
  • You have typo here: elif duration.str.,contains('Season'):. Commented May 16, 2020 at 11:00
  • You can use the in operator to check whether a string is contained within another string. e.g. if 'min' in duration.str Commented May 16, 2020 at 11:02

1 Answer 1

2

Use in operator to check wheter string contains substring ([substring] in [string])

>>> "a" in "abc"
True
>>> "ab" in "abc"
True
>>> "d" in "abc"
False
>>> x = "abc"
>>> y = "a"
>>> y in x
True
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.