gpt4 book ai didi

python - Pandas 根据日期范围和另一列过滤数据框

转载 作者:行者123 更新时间:2023-12-01 01:02:36 24 4
gpt4 key购买 nike

我有一个名为 df1pandas 数据帧,并希望根据数据帧 df2 中的条件过滤数据帧,其中对于特定的 grp_id,我只想要从 df2 中的 year 列开始到最近一年(2016)的日期,如 df3< 中所示。这只是我的数据的一个子集,其中至少有 10 个具有不同开始年份的唯一 grp_id 子集。

df1

       db_id           cert_status grp_id       year   cap prov
130 IX-011 not-certified member SD 2004-01-01 30.0 KB
131 IX-011 not-certified member SD 2005-01-01 30.0 KB
132 IX-011 not-certified member SD 2006-01-01 30.0 KB
133 IX-011 not-certified member SD 2007-01-01 30.0 KB
134 IX-011 not-certified member SD 2008-01-01 30.0 KB
135 IX-011 not-certified member SD 2009-01-01 30.0 KB
136 IX-011 not-certified member SD 2010-01-01 30.0 KB
137 IX-011 not-certified member SD 2011-01-01 30.0 KB
138 IX-011 not-certified member SD 2012-01-01 30.0 KB
139 IX-011 not-certified member SD 2013-01-01 30.0 KB
140 IX-011 not-certified member SD 2014-01-01 30.0 KB
141 IX-011 not-certified member SD 2015-01-01 30.0 KB
142 IX-011 not-certified member SD 2016-01-01 30.0 KB
208 IX-017 not-certified member CG 2004-01-01 30.0 KB
209 IX-017 not-certified member CG 2005-01-01 30.0 KB
210 IX-017 not-certified member CG 2006-01-01 30.0 KB
211 IX-017 not-certified member CG 2007-01-01 30.0 KB
212 IX-017 not-certified member CG 2008-01-01 30.0 KB
213 IX-017 not-certified member CG 2009-01-01 30.0 KB
214 IX-017 not-certified member CG 2010-01-01 30.0 KB
215 IX-017 not-certified member CG 2011-01-01 30.0 KB
216 IX-017 not-certified member CG 2012-01-01 30.0 KB
217 IX-017 not-certified member CG 2013-01-01 80.0 KB
218 IX-017 not-certified member CG 2014-01-01 30.0 KB
219 IX-017 not-certified member CG 2015-01-01 30.0 KB
220 IX-017 not-certified member CG 2016-01-01 30.0 KB

df2

   grp_id member       year
4 SD Y 2007-01-01
6 CG Y 2011-01-01

df3

       db_id           cert_status grp_id       year   cap prov
133 IX-011 not-certified member SD 2007-01-01 30.0 KB
134 IX-011 not-certified member SD 2008-01-01 30.0 KB
135 IX-011 not-certified member SD 2009-01-01 30.0 KB
136 IX-011 not-certified member SD 2010-01-01 30.0 KB
137 IX-011 not-certified member SD 2011-01-01 30.0 KB
138 IX-011 not-certified member SD 2012-01-01 30.0 KB
139 IX-011 not-certified member SD 2013-01-01 30.0 KB
140 IX-011 not-certified member SD 2014-01-01 30.0 KB
141 IX-011 not-certified member SD 2015-01-01 30.0 KB
142 IX-011 not-certified member SD 2016-01-01 30.0 KB
215 IX-017 not-certified member CG 2011-01-01 30.0 KB
216 IX-017 not-certified member CG 2012-01-01 30.0 KB
217 IX-017 not-certified member CG 2013-01-01 80.0 KB
218 IX-017 not-certified member CG 2014-01-01 30.0 KB
219 IX-017 not-certified member CG 2015-01-01 30.0 KB
220 IX-017 not-certified member CG 2016-01-01 30.0 KB

最简单、最快的方法是什么?

最佳答案

尝试使用 mergequery 进行过滤:

df1.merge(df2, on = ['grp_id'], suffixes=('','_2'), right_index=True)\
.query('year >= year_2')[df1.columns]

输出:

      db_id           cert_status grp_id        year   cap prov
133 IX-011 not-certified member SD 2007-01-01 30.0 KB
134 IX-011 not-certified member SD 2008-01-01 30.0 KB
135 IX-011 not-certified member SD 2009-01-01 30.0 KB
136 IX-011 not-certified member SD 2010-01-01 30.0 KB
137 IX-011 not-certified member SD 2011-01-01 30.0 KB
138 IX-011 not-certified member SD 2012-01-01 30.0 KB
139 IX-011 not-certified member SD 2013-01-01 30.0 KB
140 IX-011 not-certified member SD 2014-01-01 30.0 KB
141 IX-011 not-certified member SD 2015-01-01 30.0 KB
142 IX-011 not-certified member SD 2016-01-01 30.0 KB
215 IX-017 not-certified member CG 2011-01-01 30.0 KB
216 IX-017 not-certified member CG 2012-01-01 30.0 KB
217 IX-017 not-certified member CG 2013-01-01 80.0 KB
218 IX-017 not-certified member CG 2014-01-01 30.0 KB
219 IX-017 not-certified member CG 2015-01-01 30.0 KB
220 IX-017 not-certified member CG 2016-01-01 30.0 KB

关于python - Pandas 根据日期范围和另一列过滤数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55656900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com