python - Efficient loop through two Pandas DataFrames -
i have 2 pandas dataframes:
the first frame contains timestamp (date + time) , 4 sets of lat/long pairs define corners of box on earth.
the second frame contains timestamp , 1 lat/long pair marks event.
i want find out, each event, whether fell inside of 'boxes on earth', , if so, ones , delta_t between timestamps.
the way can think of doing loop through second frame, row row, , compare first frame. i'm hoping more pythonic way, nothing coming mind. ideas?
thanks,
sh
here similar question: iterating through pandas dataframe
i think work. pseudo codish. need 0.11-dev
both frames have a datetime index, first one, b shorter has events.
on a, set index column, a['date'] = a.index
on b, set index column, b['date'] = b.index
inner join , b, ffill on b
c = a.join(b).ffill()
then want boolean logic if event included, like:
c['is_included'] = (c['a_lat'] > c['b_lat']) & (c['a_long'] > c['b_long'])...etc
so when is_included true, want differential time previous event
set non-selected events nan on original date column of b
c['date_b'][~c['is_included']] = np.nan
forward fill date_b's again
c['date_b'] = c['date_b'].ffill()
if subtract date_b date_a think have timedelta
c['delta'] = c['date_b']-c['date_a']
then
c[c['is_included']] answer, , delta timedelta
Comments
Post a Comment