Home > Software design >  Index rows in Pandas Dataframe not in List of Indexes (Python) [duplicate]
Index rows in Pandas Dataframe not in List of Indexes (Python) [duplicate]

Time:09-17

I am trying to index rows of Dataframe with rows not included in the list. For example, here we extract rows 1,2 and 4 of the dataframe (data). But I would like to extract everything but those rows (say rows 3, 5, and 6):

idx = [1,3,4]
subset= data.iloc[idx , :]

So far, I tried this but get the error below:

try = data.iloc[not idx, :]
ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types

CodePudding user response:

Try this:

data.loc[~ data.index.isin(idx)]
  • Related