Home > OS >  Dropping dataframe rows based on conditional
Dropping dataframe rows based on conditional

Time:12-23

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)

CodePudding user response:

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
  • Related