1

I have a DataFrame below:

data = [['tommy', 'manny'], ['krishan', 'sony'],
       ['nick', 'nady'], ['julie', 'stephen']

df = pd.DataFrame(data, columns =['First_Name', 'Last_Name'])

I would like to drop off rows if their 'Last_Name' column has a len() of < 5. Which in this case, rows containing Krishan and Nick would be dropped. But I'm unable to do it, any advices? Thank you.

Methods tried:

I have tried this code and other similar variations but it didn't work.

df.drop(df[len(df['Last_Name']) < 5].index, inplace = True)

1 Answer 1

1

You can use .str.len():

df = df[df["Last_Name"].str.len() >= 5]

This outputs:

  First_Name Last_Name
0      tommy     manny
3      julie   stephen
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.