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

2024-10-03 17:24:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我从CSV文件加载了两个数据帧:

时间:包括我想要的所有日期,如下所示

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中的日期不等于time中的日期的行

已尝试:

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


Tags: 文件csv数据目标dfdatetime时间