I am looking for a solution to the following problem. There's a DataFrame:
data = np.array([['', 'col1', 'col2'],
['row1', 1, 2],
['row2', 3, 4]])
df = pd.DataFrame(data=data[1:,1:], index=data[1:,0],columns=data[0,1:])
I wish to retain rows in which, for example, value in column col1 belongs to a list [1, 2] while value in column col2 belongs to a list [2, 4]. This is what I thought would work
df1 = df[df['col1'].isin([1,2]) & df['col2'].isin([2,4])]
However df1 prints as an Empty DataFrame.
On the other hand, this approach
df1 = df[(df.col1 in [1,2]) & (df.col2 in [2,4])]
results in
ValueError: The truth value of a Series is ambiguous. Use a.empty, `a.bool()`, `a.item()`, `a.any()` or `a.all()`.
It would be expected to get a DataFrame with row1 in it. Needless to say I am relatively new to Python. Thanks a lot for your help.
objectdtype (because you initially had a NumPy array mixing strings and integers). For instancedf = df.astype(int)and your query will work.