Pandas:根据多个列从数据框中删除观察结果

时间:2016-07-09 13:27:02

标签: python pandas row

我有一个数据框,我需要根据其他列的值删除它的某些观察结果。

  cid      date     unemployment    billsum     id.thomas   loansum     feccanid    amtsum
N00003147   2005.0      5.6           1.0          1336        2.0      S4TN00153   4.500
N00009082   2007.0      3.7           1.0            11        2.0      S6CO00168   13.000
N00013870   2007.0      4.6           3.0          1697        17.5     S2MN00126   1636.709
N00002091   2007.0      3.1           1.0           246        11.5     S0ID00057   238.795
N00006515   2007.0      3.8           2.0          1319        49.5     S8NM00010   966.286

我想,例如仅在id.thomas == 1763(我的日期范围是2005-14)时删除date == 2008 through 2012的值。 我试过了:

bill_amtmerge = bill_amtmerge[bill_amtmerge['id.thomas']!= 1763 & (bill_amtmerge['date'] > 2007)]

有人对此有所了解吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

mask = (df['id.thomas'] == 1763) & (df['date'] >= 2008) & (df['date'] <= 2012)
df = df[~mask]

或者你可以negate your condition

df = df[(df['id.thomas'] != 1763) | (df['date'] < 2008) | (df['date'] > 2012)]