Tianhe Xie
Tianhe Xie

Reputation: 261

How to drop all rows where the first column contains a specific character without referencing column name?

With a data frame, I want to drop all rows where the first columns contains the string or substring 'XYZ'. I know this works:

df[~df.NameOfColumn.str.contains("XYZ")]

However, I do not want to use the name of the first column. (This is because I have lots of dataframes and the names of their first column is different). Any idea how to do that?

Upvotes: 1

Views: 102

Answers (1)

jezrael
jezrael

Reputation: 862751

Use DataFrame.iloc for select first column by position 0, here : means select all rows:

df[~df.iloc[:, 0].str.contains("XYZ")]

Upvotes: 1

Related Questions