在以下数据框中,我想找到与日期对应的列的特定值。假设我需要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
这样就可以得到air_temp = 11.00。
这样做的“Pandasic”方式是什么?提前致谢
答案 0 :(得分:3)
使用df.loc
:
df.loc[df["date"] == "2005-12-22", "AIR_TEMP"]
答案 1 :(得分:2)
print(df[df['date'] == '2005-12-22']['AIR_TEMP'])
答案 2 :(得分:1)
如果'date'在索引中,那么您可以使用loc
在日期时间索引上使用partial string indexing。
df.loc['2005-12-22','AIR_TEMP']
输出:
11.0
df是:
<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