我有一个看起来像这样的数据框:
Date Location NO2
2017-11-24 23:00:00 toronto 0.038
2017-11-24 22:00:00 toronto 0.031
2017-11-24 21:00:00 toronto 0.025
2017-11-24 20:00:00 toronto 0.033
2017-11-24 19:00:00 toronto 0.026
2017-11-24 18:00:00 toronto 0.021
2017-11-24 17:00:00 toronto 0.017
每周24小时每天记录一次。在这段时间内如何找到NO2的最高值?
答案 0 :(得分:1)
您可以使用np.where()
:
导入数据:
import sys
if sys.version_info[0] < 3:
from StringIO import StringIO
else:
from io import StringIO
data = StringIO('''Date,Location,NO2
2017-11-24 23:00:00,toronto,0.038
2017-11-24 22:00:00,toronto,0.031
2017-11-24 21:00:00,toronto,0.025
2017-11-24 20:00:00,toronto,0.033
2017-11-24 19:00:00,toronto,0.026
2017-11-24 18:00:00,toronto,0.021
2017-11-24 17:00:00,toronto,0.017''')
df = pd.read_csv(data, sep=',')
使用np.where()
查找与最大NO2值匹配的行的索引:
max_time = df.loc[np.where(df.NO2.values == df.NO2.max())[0], 'Date'].values[0]
max_time = df.loc[np.where(df.NO2.values == df.NO2.max())[0], 'Date'].values[0]
print('Max time:',max_time)
print('Max NO2:',df.NO2.max())
Max time: 2017-11-24 23:00:00
Max NO2: 0.038
答案 1 :(得分:1)
您可以创建带有DatetimeIndex的时间序列,并使用Series.idxmax
来创建日期,最大值为NO
,使用max
来创建最大值:
s = df.set_index('Date')['NO2']
print (s.idxmax())
2017-11-24 23:00:00
print (s.max())
0.038
如果需要每天最多的日期:
print (df)
Date Location NO2
0 2017-11-24 23:00:00 toronto 0.038
1 2017-11-24 22:00:00 toronto 0.031
2 2017-11-24 21:00:00 toronto 0.025
3 2017-11-25 20:00:00 toronto 0.033
4 2017-11-25 19:00:00 toronto 0.026
5 2017-11-26 18:00:00 toronto 0.021
6 2017-11-26 17:00:00 toronto 0.017
df1 = df.set_index('Date').groupby(pd.Grouper(freq='24H'))['NO2'].idxmax().reset_index()
print (df1)
Date NO2
0 2017-11-24 2017-11-24 23:00:00
1 2017-11-25 2017-11-25 20:00:00
2 2017-11-26 2017-11-26 18:00:00
df2 = (df.set_index('Date')
.groupby(pd.Grouper(freq='24H'))['NO2']
.agg([('maxdate','idxmax'),('maxval','max')]))
print (df2)
maxdate maxval
Date
2017-11-24 2017-11-24 23:00:00 0.038
2017-11-25 2017-11-25 20:00:00 0.033
2017-11-26 2017-11-26 18:00:00 0.021
或者如果需要最大时间:
print (df)
Date Location NO2
0 2017-11-24 23:00:00 toronto 0.038
1 2017-11-24 22:00:00 toronto 0.031
2 2017-11-24 21:00:00 toronto 0.025
3 2017-11-25 20:00:00 toronto 0.033
4 2017-11-25 21:00:00 toronto 0.026
5 2017-11-26 21:00:00 toronto 0.021
6 2017-11-26 22:00:00 toronto 0.017
s = (df.groupby(df['Date'].dt.time)['NO2'].mean())
print (s)
Date
20:00:00 0.033
21:00:00 0.024
22:00:00 0.024
23:00:00 0.038
Name: NO2, dtype: float64
print (s.idxmax())
23:00:00
print (s.max())
0.038
答案 2 :(得分:0)
尝试一下:
df.iloc[np.argmax(df.NO2),:]