大熊猫的元素不在索引列表中

hel*_*lrd 3 python indexing pandas

我怎样才能获得不在给定索引列表中的pandas DataFrame的元素?

一个简单的例子:

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])
Run Code Online (Sandbox Code Playgroud)

所以例如,现在我想获得所有不在ind中的元素(所以索引0,5,6,7,8,9)......

谢谢您的帮助!

jez*_*ael 5

用途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
Run Code Online (Sandbox Code Playgroud)