Behnam
Behnam

Reputation: 511

find a value corresponding to a date in pandas

In the following dataframe, I would like to find a certain value of a column corresponding to a date. Let's say I need air temperature of the day 2005-12-22:

date    AIR_TEMP    ATM_PRESS   REL_HUM WIND_DIR    WIND_SPEED
2005-12-16  10.1    102935.4912 0.81    231.8   3.5
2005-12-17  9.75    103355.1512 0.72    273.5   3.3
2005-12-18  9.77    103522.4912 0.64    291.6   1.2
2005-12-19  10.74   103098.1512 0.77    234.5   3
2005-12-20  10.99   102807.8212 0.85    253.7   3.1
2005-12-21  11.11   102622.4912 0.74    263.5   2.6
2005-12-22  11.00   102863.4912 0.77    269.3   1.2
2005-12-23  10.91   103232.8212 0.79    277.0   3
2005-12-24  10.66   103420.8212 0.67    328.8   2.4

so that air_temp=11.00 would be the result.

What would be a 'Pandasic' way of doing this? Thanks in advance

Upvotes: 2

Views: 5190

Answers (3)

Scott Boston
Scott Boston

Reputation: 153460

If 'date' is in the index, then you can use partial string indexing on a datetime index using loc.

df.loc['2005-12-22','AIR_TEMP']

Output:

11.0

Where df is:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 9 entries, 2005-12-16 to 2005-12-24
Data columns (total 5 columns):
AIR_TEMP      9 non-null float64
ATM_PRESS     9 non-null float64
REL_HUM       9 non-null float64
WIND_DIR      9 non-null float64
WIND_SPEED    9 non-null float64
dtypes: float64(5)
memory usage: 752.0 bytes

Upvotes: 3

koPytok
koPytok

Reputation: 3713

Use df.loc:

df.loc[df["date"] == "2005-12-22", "AIR_TEMP"]

Upvotes: 3

Nihal
Nihal

Reputation: 5334

print(df[df['date'] == '2005-12-22']['AIR_TEMP'])

Upvotes: 2

Related Questions