我的csv有两列CURRENT_PLAN_START_DATE和CURRENT_PLAN_END_DATE。我想返回在7/17/16之后具有CURRENT_PLAN_START_DATE日期的所有行以及在7/31/16之前具有CURRENT_PLAN_END_DATE的所有行。我尝试通过使用python datetime解析7/1/16和7/31/16字符串来做到这一点,但仍然遇到类型不匹配,所以我在电子表格中添加了两列,反复复制这些字符串。这就是我到目前为止所做的:
import pandas as pd
df = pd.read_csv('/Users/Margaret/Desktop/CP1.csv')
start_dates = pd.to_datetime(df['CURRENT_PLAN_START_DATE'])
end_dates = pd.to_datetime(df['CURRENT_PLAN_END_DATE'])
before_date = pd.to_datetime(df['BEFORE_DATE'])
after_date = pd.to_datetime(df['AFTER_DATE'])
#print(start_dates.sort_values())
#print(df.sort_values(by=['CURRENT_PLAN_START_DATE']))
x = df[(df['CURRENT_PLAN_START_DATE'] > df['BEFORE_DATE'][1]) & (df['CURRENT_PLAN_END_DATE'] > df['AFTER_DATE'][1])]
答案 0 :(得分:0)
你可以试试这个
df['CURRENT_PLAN_START_DATE'] = pd.to_datetime(df['CURRENT_PLAN_START_DATE'])
df['CURRENT_PLAN_END_DATE'] = pd.to_datetime(df['CURRENT_PLAN_END_DATE'])
df=df.loc[(df['CURRENT_PLAN_START_DATE']>pd.to_datetime('2016-07-01'))&(df['CURRENT_PLAN_END_DATE']<pd.to_datetime('2016-07-31')),:]