How to select the inverse index in pd.DataFrame by using loc
or iloc
?
I tried df.loc[!my_index,my_feature]
but fail.
And df.loc[[ind for ind in df.index.tolist() if ind not in my_index],my_feature]
looks too dull. Any better idea?
Using loc() function to Reverse Row Reversing the rows of a data frame in pandas can be done in python by invoking the loc() function. The panda's dataframe. loc() attribute accesses a set of rows and columns in the given data frame by either a label or a boolean array.
To get the index of a Pandas DataFrame, call DataFrame. index property. The DataFrame. index property returns an Index object representing the index of this DataFrame.
To change the index values we need to use the set_index method which is available in pandas allows specifying the indexes. where, inplace parameter accepts True or False, which specifies that change in index is permanent or temporary. True indicates that change is Permanent.
Definition and Usage. The index property returns the index information of the DataFrame. The index information contains the labels of the rows. If the rows has NOT named indexes, the index property returns a RangeIndex object with the start, stop, and step values.
Use difference
:
df.loc[df.index.difference(my_index),my_feature]
Alternatively numpy.setdiff1d
:
df.loc[np.setdiff1d(df.index, my_index),my_feature]
Sample:
my_index = [5,7]
df = pd.DataFrame({'A': ['a','a','a','b'], 'B': list(range(4)) }, index=[5,7,8,9])
print (df)
A B
5 a 0
7 a 1
8 a 2
9 b 3
print(df.loc[df.index.difference(my_index),'A'])
8 a
9 b
Name: A, dtype: object
You may take advantage of index.difference
.
idx2 = df.index.difference(my_index)
Or, set.difference
idx2 = set(df.index).difference(my_index) # note, order not guaranteed
df.loc[idx2, ...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With