1

I have a dataset, df, that has several columns where I would like to pinpoint and extract where the value is a certain length (if it exceeds 10 characters) and to remove this row

data

ID             type
abccccc        22
aaaaaaaaaaa    22
b              11

desired

ID             type
abccccc        22
b              11

doing

df.drop(df.index[df['ID'] == >10

I am still researching this, any assistance is appreciated

Lynn
  • 4,292
  • 5
  • 21
  • 44
  • Think [this](https://stackoverflow.com/questions/42895061/how-to-remove-a-row-from-pandas-dataframe-based-on-the-length-of-the-column-valu) is related to what you need. – Gerhard Mar 28 '21 at 19:25

1 Answers1

2

You can use:

df[df['ID'].str.len() <= 10]
bb1
  • 7,174
  • 2
  • 8
  • 23
  • Thank you, is there a way to show what row was dropped? – Lynn Mar 28 '21 at 19:25
  • 1
    If you want to get the rows where the value of the ID exceeds 10 characters, you can execute `df[df['ID'].str.len() > 10]`. Alternatively, you can add a column to your dataframe as follows `df["long_ID"] = df['ID'].str.len() > 10`. This columns will have True values in rows with IDs exceeding 10 characters and False values in the other rows. – bb1 Mar 28 '21 at 19:31
  • 1
    @Lynn: Alternatively, you can use `index.to_list()` to get the list of indices matching the condition: https://www.delftstack.com/howto/python-pandas/how-to-get-index-of-all-rows-whose-particular-column-satisfies-given-condition-in-pandas/ – SKPS Mar 28 '21 at 19:36