I have a dataframe df like this:
col1 | col2
a | [1,2]
b | [3,4]
c | [3,9]
I want to get the row based on a matching input array, so if I have the array [1,2], I can get:
col1 | col2
a | [1,2]
When I try to do this using this formula, it doesn't work:
df.loc[df['Col2'] == [1,2]]
Error: Lengths must match to compare
The real cause of your error was that not all lists are of the same size, this causes issues with DataFrame.eq
.
The best approach to tackling this is to build a boolean mask using a list comprehension and then use it to index into df
:
df[[v == [1, 2] for v in df['col2'].tolist()]]
Another alternative is df.apply
, but that isn't nearly as fast as this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.