Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elements of pandas not in an index list [duplicate]

How could I get the elements of a pandas DataFrame that are not in a given list of index?

A simple example:

import pandas as pd
import numpy as np

A = np.linspace(10, 100, 10)

A = pd.DataFrame(A, columns=["A"])
ind = [x for x in range(1, 4)]
print(A.iloc[ind])

So for example, now I would like to get all the elements that are not in ind (so indexes 0,5,6,7,8,9)...

Thanks for the help!

like image 483
hellowolrd Avatar asked Oct 16 '25 08:10

hellowolrd


1 Answers

Use Index.difference:

print(A.iloc[A.index.difference(ind)])
       A
0   10.0
4   50.0
5   60.0
6   70.0
7   80.0
8   90.0
9  100.0
like image 159
jezrael Avatar answered Oct 17 '25 23:10

jezrael