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?
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?
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