I have a dataframe with lots of columns and one of them is a list of IDs.
eg
| IDs | IPs | ...
1 |[12, 55, 15] | 123... | ...
2 |[12] | 222... | ...
3 |[10, 11, 5] | 234... | ...
4 |[12, 55, 15] | 123... | ...
5 |[12, 55, 15] | 123... | ...
I have another list
l1 = [12 , 15, 55]
The results I want
| IDs | IPs | ...
1 |[12, 55, 15] | 123... | ...
4 |[12, 55, 15] | 123... | ...
5 |[12, 55, 15] | 123... | ...
I have tried using .isin
, .query
, and tried to other solutions online but none of them worked for me. The result I gotten are empty dataframes and
0 False
1 False
2 False
3 False
4 False
edit: The order is not important. I just want the numbers in the list to be in the result.
You can compare tuple
s if order is important:
import ast
#if necessary convert strings to lists
df['IDs'] = df['IDs'].apply(ast.literal_eval)
mask = df['IDs'].apply(tuple).eq(tuple(l1))
Use apply lambda
if order is important.
df.IDs.apply(lambda x: x == l1)
This a solution I have found:
Edit: Actually really close solution from Hamza usman ghani's one. What is wrong exactly with his?
import pandas as pd
df = pd.DataFrame(data={"column":[[12, 55, 15],[12, 55, 15],[12],[12, 55, 15]],"value":[5,5,1,5]})
print(df["column"].apply(lambda x : x==[12,55,15])) # prints {0,True ... }
print(df[df["column"].apply(lambda x : x==[12,55,15])]) # prints what you want
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.