2

I have a pandas dataframe such as follow:

import numpy as np
pd.DataFrame(np.random.rand(20,2))

I would like to remove from it the rows with index contained in the list:

list_ = [0,4,5,6,18]

How would I go about that?

2 Answers 2

2

Use drop:

df = df.drop(list_)
print (df)
           0         1
1   0.311202  0.435528
2   0.225144  0.322580
3   0.165907  0.096357
7   0.925991  0.362254
8   0.777936  0.552340
9   0.445319  0.672854
10  0.919434  0.642704
11  0.751429  0.220296
12  0.425344  0.706789
13  0.708401  0.497214
14  0.110249  0.910790
15  0.972444  0.817364
16  0.108596  0.921172
17  0.299082  0.433137
19  0.234337  0.163187
Sign up to request clarification or add additional context in comments.

Comments

1

This will do it:

remove = df.index.isin(list_)
df[~remove]

Or just:

df[~df.index.isin(list_)]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.