JamesHudson81
JamesHudson81

Reputation: 2273

dropping rows in a df based condition in the index

I have this df:

              A
migeludis     2
sandradsad    3
luislkdksd    4
juandsasdi    5

I would like to drop the rows in the df those index begin with luis and sandra to obtain this output:

              A
migeludis     2
juandsasdi    5

Upvotes: 1

Views: 28

Answers (1)

jezrael
jezrael

Reputation: 862801

Use boolean indexing with str.startswith and inverse condition by ~:

df = df[~df.index.str.startswith(('luis','sandra'))]
print (df)
            A
migeludis   2
juandsasdi  5

Upvotes: 1

Related Questions