Filter data frame based on index value in Python
Question:
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
Answers:
try this:
Filter_df = df[df.index.isin(my_list)]
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
try this:
Filter_df = df[df.index.isin(my_list)]