user7096526
user7096526

Reputation:

looking for value in a panda dataframe python

I have a data frame with three columns,

    A       B     C
    One     2     1
    Two     3     0.5
    Three   6     7

I have a list which represents the second and third columns: [(3,0.5),(6,7),(2,1)]

based on that list I want to retrieve the value in the first columns as a list: [Two, Three, One]

How can I do that? Thank you in advance!!

Upvotes: 1

Views: 81

Answers (1)

akuiper
akuiper

Reputation: 215117

You can set columns B and C as multi index and then query it with the list:

idx = [(3,0.5),(6,7),(2,1)]
df.set_index(['B', 'C']).A.loc[idx].values
# array(['Two', 'Three', 'One'], dtype=object)

If you need a list as result, use tolist as @Jezrael's comment.

Upvotes: 3

Related Questions