user2100039
user2100039

Reputation: 1366

Find Pandas Rows Not Matching Hours and Delete Unmatched Rows

I am trying to align the rows of two pandas based on the date rows of one panda and then delete/drop the unmatched rows. For example, my data look like this in df1:

     Site    Data    hour    day    month    year
0    site1   5.4     1       1      1        2020
1    site1   4.6     2       1      1        2020
2    site1   3.7     4       1      1        2020

And, the data in df2:

     Site    Data    hour    day    month    year
0    site1   4.9     1       1      1        2020
1    site1   4.1     2       1      1        2020
2    site1   6.2     3       1      1        2020
3    site1   5.1     4       1      1        2020

I need to apply an index perhaps on the "hour" column and I've tried boolean indexing to identify where the dates in 'hour' do not match but I do not know how to drop that row in df2 so that after the drop rows, df2 looks like this after the reset_index(). My goal is to make the length of df1 = length of df2 after the row(s) drop. I need df2 to look like this:

     Site    Data    hour    day    month    year
0    site1   4.9     1       1      1        2020
1    site1   4.1     2       1      1        2020
2    site1   5.1     4       1      1        2020

Thank you,

Upvotes: 0

Views: 78

Answers (1)

wwnde
wwnde

Reputation: 26686

Use .isin()

df2[df2['hour'].isin(df1['hour'])]



 Site  Data  hour  day  month  year
0  site1   4.9     1    1      1  2020
1  site1   4.1     2    1      1  2020
3  site1   5.1     4    1      1  2020

Upvotes: 1

Related Questions