Reputation: 157
I have a (probably dumb) issue with pandas' tables. Let's say I have a table with, among the others, a column named 'lett', that includes a single letter for each row, and a column named 'num', with a number. I also have the following 2 lists
letter=["A", "B", "C"]
number=[[1, 2], [3, 4], [5, 6]]
Now, I want to extract all the rows with A in the letter column and 1 or 2 in the number column, B with 3 or 4 and C with 5 or 6.
My idea was to run
table['num'].isin(number[letter.index(table['lett'])])
But I get the following issue: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). (maybe because table['lett'] does not refer to the specific row, but to the whole possible values included in the 'lett' column?). What am I supposed to do? Thanks in advance
Upvotes: 0
Views: 46
Reputation: 323226
First make your two list
s to list
of tuple
, then make your columns
number and letter to a tuple
as well , then we can using isin
tup=[(x,z)for x , y in zip(letter,number) for z in y ]
yourdf=df[df[['letter','Number']].apply(tuple,axis = 1).isin(tup)]
Or using merge
pd.DataFrame(tup,columns=['letter','number']).merge(df,how='left')
Upvotes: 2