Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter data frame based on index value in Python [duplicate]

Tags:

python

pandas

I have a data frame df with thousands of rows, and a sample is this:

    Index           A   B   C   D   E   F                    EX-A.1.A.B-1A  18   7   2   2   9   8            EX-A.1.A.B-1C   0   0   0   0   0   0            EX-A.1.A.B-4A   6   4   8   6   1   1        EX-A.1.A.B-4C   0   0   0   0   0   0        EX-A.1.A.B-4F   0   0   0   0   0   0 

I also have a list my_list = ["EX-A.1.A.B-1A","EX-A.1.A.B-4A","EX-A.1.A.B-4F"]

and I want to filter the df based on this list, therefore I want to keep the rows for which the index value is in the list my_list.

I tried this in order to create a new filtered df: Filter_df = df[df.index in my_list] and I get this error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(). 

Any ideas on how I could do this? Thanks

like image 239
RGRGRG Avatar asked Nov 26 '18 14:11

RGRGRG


People also ask

Can Pandas index have duplicates?

duplicated() function Indicate duplicate index values. Duplicated values are indicated as True values in the resulting array. Either all duplicates, all except the first, or all except the last occurrence of duplicates can be indicated.

How do you find duplicates in a data frame?

duplicated() method is used to find duplicate rows in a DataFrame. It returns a boolean series which identifies whether a row is duplicate or unique.

Does drop duplicates include index?

drop_duplicates() function to drop all the occurrences of the duplicate value except the first occurrence. Output : Let's drop all occurrences of duplicate value in the Index except the first occurrence.


1 Answers

try this:

Filter_df  = df[df.index.isin(my_list)] 
like image 69
Ala Tarighati Avatar answered Sep 19 '22 18:09

Ala Tarighati