我有一个数据帧,温度为:
temp.ix[1:10]
KCRP
DateTime
2011-01-01 01:00:00 61.0
2011-01-01 02:00:00 60.0
2011-01-01 03:00:00 57.0
2011-01-01 04:00:00 56.0
2011-01-01 05:00:00 51.0
2011-01-01 06:00:00 55.0
2011-01-01 07:00:00 65.0
2011-01-01 08:00:00 55.0
2011-01-01 09:00:00 55.0
我还有一个数据框df
,是:
df[['Start Time', 'End Time']].ix[1:10]
Start Time End Time
DateTime
2011-01-23 05:00:00 2011-01-01 05:00:00 2011-01-01 06:11:00
2011-01-25 04:00:00 2011-01-25 04:51:00 2011-01-26 00:19:00
2011-01-26 04:00:00 2011-01-26 04:29:00 2011-01-26 23:13:00
2011-02-03 07:00:00 2011-02-03 07:56:00 2011-02-03 08:11:00
2011-02-12 19:00:00 2011-02-12 19:52:00 2011-02-13 12:14:00
2011-02-15 14:00:00 2011-02-15 14:09:00 2011-02-15 14:22:00
2011-02-22 05:00:00 2011-02-22 05:47:00 2011-02-22 05:55:00
2011-02-26 06:00:00 2011-02-26 06:47:00 2011-02-26 07:25:00
2011-03-01 00:00:00 2011-03-01 00:44:00 2011-03-02 00:11:00
对于df
的每一行,我想从temp
中选择最大值,然后从temp
中提取Start Time
和{{1}之间的所有值。 }。
因此,对于df的第一行,我的答案将是:
End Time
除了遍历df[['Start Time', 'End Time']].ix[1:10]
Start Time End Time Max Temp
DateTime
2011-01-23 05:00:00 2011-01-01 05:00:00 2011-01-01 06:11:00 55
的每一行外,我不确定如何进行此操作,这可能不是一种有趣的方法。
我尝试过:
df
答案 0 :(得分:1)
一种简单的方法是使用apply
:
def get_max_temp(row):
return max(temp[(temp['DateTime'] >= row['Start_Time']) & (temp['DateTime'] <= row['End_Time'])]['KCRP'])
df['Max_Temp'] = df.apply(get_max_temp, axis=1)
您也可以使用矢量化函数以获得更好的性能,但是显式遍历数据帧中的行应该几乎总是最后一个选择。
更新:
矢量版本:
def get_max_temp(start, end):
return max(temp[(temp['DateTime'] >= start) & (temp['DateTime'] <= end)]['KCRP'])
get_max_temp = np.vectorize(get_max_temp)
df['Max_Temp'] = get_max_temp(df['Start_Time'], df['End_Time'])