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
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.
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.
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.
try this:
Filter_df = df[df.index.isin(my_list)]
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