Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select the inverse index in pd.Dataframe

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?

like image 746
Garvey Avatar asked Apr 16 '18 10:04

Garvey


People also ask

How do you reverse a DataFrame index?

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.

How do I find the index of a DataFrame in PD?

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.

How do I change the index of a DataFrame in PD?

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.

What is PD DataFrame index?

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.


2 Answers

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
like image 93
jezrael Avatar answered Oct 20 '22 10:10

jezrael


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, ...]
like image 34
cs95 Avatar answered Oct 20 '22 10:10

cs95