Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

python - Dataframe Index row comparison and removal

Good day,

I need a bit of advice I have two dataframes. One containing hundrends of lines and another containing six. Timestamps are indexed for both frames. I want to compare index values in df1 with df2 and keep only the rows that match the timestamp in df2.

df1
Date-Time                A    B    C    D
2012-01-11 7:10:05:00    0    1    2    3
2012-01-11 7:14:05:00    0    1    2    3
2012-01-11 7:16:05:00    0    1    2    3
2012-01-11 7:25:05:00    0    1    2    3
2012-01-11 7:31:05:00    0    1    2    3

df2
Date-Time                A    B    C    D
2012-01-11 7:10:05:00    6    8    1.5  6.8
2012-01-11 7:25:05:00    0    11   4    3.3
2012-01-11 7:31:05:00    0    44   2.2  3.3

So only rows 0, 3 and 4 should be remaining in d1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Make sure your 'Date-Time' column is the index in both (you can get it by set_index(), and then pick 1:

1) Use isin:

df1_new = df1[df1.index.isin(df2.index)]

2) Use loc:

df1_new = df1.loc[df2.index, :]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...