在使用Python处理数据时,经常需要对数据筛选。
这是在对时间筛选时,判断两列时间是否相差一年,如果是,则返回符合条件的所有列。
data原始数据:
data[map(lambda x:datetime.date(x.year-1,x.month,x.day),data['report_date'])==data['date_1y_ago']] company_id signal_code_x signal_value_x report_date signal_code_y signal_value_y report_date_last date_1y_ago 0 2154888 r_1002030000 0.62660 2015-09-30 r_1002030000 0.64145 2015-12-31 2014-12-31 1 2154888 r_1002030000 0.64145 2015-12-31 r_1002030000 0.64145 2015-12-31 2014-12-31 2 2154888 r_1002030000 0.60544 2015-03-31 r_1002030000 0.64145 2015-12-31 2014-12-31 3 2154888 r_1002030000 0.54911 2014-12-31 r_1002030000 0.64145 2015-12-31 2014-12-31 4 2154888 r_1002030000 0.61379 2015-06-30 r_1002030000 0.64145 2015-12-31 2014-12-31 5 1702887 r_1002030000 0.62173 2014-03-31 r_1002030000 0.51103 2015-03-31 2014-03-31 6 1702887 r_1002030000 0.55175 2014-12-31 r_1002030000 0.51103 2015-03-31 2014-03-31 7 1702887 r_1002030000 0.51103 2015-03-31 r_1002030000 0.51103 2015-03-31 2014-03-31 8 1702887 r_1002030000 0.58696 2014-06-30 r_1002030000 0.51103 2015-03-31 2014-03-31 9 13484491 r_1002030000 0.17658 2014-12-31 r_1002030000 0.41082 2015-12-31 2014-12-31 10 13484491 r_1002030000 0.41082 2015-12-31 r_1002030000 0.41082 2015-12-31 2014-12-31 11 13484491 r_1002030000 0.39220 2015-09-30 r_1002030000 0.41082 2015-12-31 2014-12-31
筛选后的数据:
company_id signal_code_x signal_value_x report_date signal_code_y signal_value_y report_date_last date_1y_ago 1 2154888 r_1002030000 0.64145 2015-12-31 r_1002030000 0.64145 2015-12-31 2014-12-31 7 1702887 r_1002030000 0.51103 2015-03-31 r_1002030000 0.51103 2015-03-31 2014-03-31 10 13484491 r_1002030000 0.41082 2015-12-31 r_1002030000 0.41082 2015-12-31 2014-12-31
以上这篇使用Python的Dataframe取两列时间值相差一年的所有行方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。