1

Let's say I have a pandas dataframe df1 and array of values a1

df1 col1 |col2 A |123 B |321 C |2323 D |3232 a1 [A,B,A,D] 

I'd like to return a dataframe df2 with len(a1) rows.

df2 col1 |col2 A |123 B |321 A |123 D |3232 

I've been trying using iloc, loc but I can't find a suitable solution...

edit: I've tried using

len(df1[df1['col1'].isin(a1)]) 

but I think this solution only returns unique rows and it doesnt duplicate them like I need it to.

0

1 Answer 1

2

You need to set_index before you can use loc with your list of values:

a1 = ['A', 'B', 'A', 'D'] df.set_index('col1').loc[a1] col2 col1 A 123 B 321 A 123 D 3232 
Sign up to request clarification or add additional context in comments.

1 Comment

it works! I'm not exactly sure how set_index works, but it works.... I found a solution using isin but that one doesnt include duplicated rows. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.