如何删除数据框中不匹配日期的行?

时间:2018-02-26 09:20:21

标签: python dataframe jupyter

我有两个从CSV文件加载的数据框:

time_df:包含我想要的所有日期,如下所示

0       2017-01-31
1       2017-01-26
2       2017-01-12
3       2017-01-09
4       2017-01-02

price_df:包含其他字段和许多我不需要的日期

Date           NYSEARCA:TPYP  NYSEARCA:MENU  NYSEARCA:SLYV  NYSEARCA:CZA
0  2017-01-31            NaN          16.56         117.75         55.96
1  2017-01-26            NaN          16.68         116.89         55.84
2  2017-01-27            NaN          16.70         118.47         56.04
3  2017-01-12            NaN          16.81         119.14         56.13
5  2017-01-09            NaN          16.91         120.00         56.26
6  2017-01-08            NaN          16.91         120.00         56.26
7  2017-01-02            NaN          16.91         120.00         56.26

我的目的是删除price_df中日期不等于time_df中日期的行

尝试:

del price_df['Date'] if price_df['Date']!=time_df['Date']

但是我不能打印print(price_df['Date']!= time_df['Date']) 但它显示下一个错误:Can only compare identically-labeled Series objects

1 个答案:

答案 0 :(得分:1)

内心join可以修复问题:

time_df.merge(price_df, on='Date',copy=False)