1

I have a dataframe with 3 columns:

ID     datetime     X
10     01/01/2018   3
10     02/01/2018   4
12     02/01/2018   8
12     07/01/2018   12

Now my question is, what is the best way to get X given an ID and date?

Crackertje
  • 11
  • 2
  • Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Dec 07 '18 at 12:36
  • Do you think [pivot](https://stackoverflow.com/questions/47152691/how-to-pivot-a-dataframe) ? – jezrael Dec 07 '18 at 12:55
  • Is `ID` the index? – yatu Dec 07 '18 at 13:02
  • No, ID is not the index – Crackertje Dec 07 '18 at 13:04

1 Answers1

1

You can use .loc. From the documentation:

.loc is primarily label based, but may also be used with a boolean array.

So you can use it for boolean indexing and combine both conditions with a bitwise AND operator, &. Note that the conditions must be separated by parenthesis.

Example ID and date:

ID = 10
date = '02/01/2018'

Indexing of the dataframe:

df.loc[(df.ID == ID) & (df.date == date), 'X']
1    4
yatu
  • 86,083
  • 12
  • 84
  • 139