Reputation: 583
I have two pandas dataframes df1 and df2 where age, Start_Time, End_Time are datetime64[ns] dtypes. I want to extract data points in df1 which are falling within any of Start_Time End_Time in df2
df1
age LAeq LSeq Doss LSeq Gliss LZeq
0 2019-05-14 15:40 62.02 NaN NaN 0.0
1 31-01-2019 15:39 60.45 NaN NaN 0.0
df2
index Start_Time End_Time Equipment_Group
3200 2019-05-14 08:00:00 2019-05-14 16:00:00 Atmospheric_Vacts
4856 2019-07-22 08:00:00 2019-07-22 16:00:00 Atmospheric_Vacts
for index, row in df2.iterrows():
start = row['Start_Time']
end = row['End_Time']
df1.loc[df1['age'].between(start, end) , 'ACTIVE'] = True
df1.head()
I am getting 'NaN' in ACTIVE column instead of True. It will be helpful, if I can get some direction on this.
Upvotes: 0
Views: 68
Reputation: 13289
Try to fill non-active values with False
:
for index, row in df2.iterrows():
start = row['Start_Time']
end = row['End_Time']
df1.loc[df1['age'].between(start, end) , 'ACTIVE'] = True
df1['ACTIVE'] = df1['ACTIVE'].fillna(False)
df1.head()
Upvotes: 1