我正在尝试在特定列中的特定日期操作 CSV 文件。我为此使用了熊猫(总菜鸟),并且在我约会之前非常成功。CSV 看起来像这样(当然有更多的列和行)。这些是列:
电路
状态
生效日期
这些是值:
XXXX001
操作
2007 年 12 月 31 日
我尝试了数据框查询(我用于其他所有内容)但没有成功。我尝试了 dataframe loc (适用于其他所有内容)但没有成功。
如何从给定日期获取所有较旧或较新的行?如果我有其他条件来过滤数据框,我如何将它们与日期过滤器结合起来?
这是我的“原始”代码:
import pandas as pd
# parse_dates = ['Effective Date']
# dtypes = {'Effective Date': 'str'}
df = pd.read_csv("example.csv", dtype=object)
# , parse_dates=parse_dates, infer_datetime_format=True
# tried lot of suggestions found on SO
cols = df.columns
cols = cols.map(lambda x: x.replace(' ', '_'))
df.columns = cols
status1 = 'Suppressed'
status2 = 'Order Aborted'
pool = '2'
region = 'EU'
date1 = '31-DEC-2017'
filt_df = df.query('Status != @status1 and Status != @status2 and Pool == @pool and Region_A == @region')
filt_df.reset_index(drop=True, inplace=True)
filt_df.to_csv('filtered.csv')
# this is working pretty well
supp_df = df.query('Status == @status1 and Effective_Date < @date1')
supp_df.reset_index(drop=True, inplace=True)
supp_df.to_csv('supp.csv')
# this is what is not working at all
Run Code Online (Sandbox Code Playgroud)
我尝试了很多方法,但我无法将它们放在一起。这只是我尝试过的众多方法之一..所以我知道这可能是完全错误的,因为没有使用日期解析。supp.csv 将被保存,但出现的日期到处都是,因此与此代码中的“逻辑”不匹配。谢谢你的帮助!
确保将日期转换为日期时间,然后对其进行过滤切片。
df['Effective Date'] = pd.to_datetime(df['Effective Date'])
df[df['Effective Date'] < '2017-12-31']
#This returns all the values with dates before 31th of December, 2017.
#You can also use Query
Run Code Online (Sandbox Code Playgroud)