我有一個非常大的數據框,我正在嘗試清理。這是一個數據框的示例。從熊貓數據框中刪除特定行
[in]: dftrain
[out]:
date store_nbr item_nbr units tavg preciptotal
2012-01-01 1 1 0 42 0.05
2012-01-01 1 2 2 42 0.05
2012-01-01 1 3 0 42 0.05
2012-01-01 1 4 0 42 0.05
2012-01-01 2 1 0 62 0.15
2012-01-01 2 2 0 62 0.15
2012-01-01 2 3 20 62 0.15
2012-01-01 2 4 1 62 0.15
2012-01-01 3 1 0 72 1.05
2012-01-01 3 2 0 72 1.05
2012-01-01 3 3 29 72 1.05
2012-01-01 3 3 0 72 1.05
...(New Day)
date store_nbr item_nbr units tavg preciptotal
2012-01-02 1 1 0 47 0.10
2012-01-02 1 2 0 47 0.10
2012-01-02 1 3 0 47 0.10
2012-01-02 1 4 0 47 0.10
2012-01-02 2 1 0 60 0.0
2012-01-02 2 2 0 60 0.0
2012-01-02 2 3 5 60 0.0
2012-01-02 2 4 0 60 0.0
2012-01-02 3 1 0 67 .23
2012-01-02 3 2 0 67 .23
2012-01-02 3 3 2 67 .23
2012-01-02 3 3 0 67 .23
...
我有一個字典,str_nbr作爲key和item_nbr作爲我想要保留的存儲和項目編號的值。這裏是一個示例字典:{'1': [2], '2': [3,4], '3': [3]}
我想要做的是去商店存儲和刪除所有行的item_nbr不包含作爲我的字典的值。因此,在這個例子中,當我已經放棄了不需要的行我會這樣...
[in]: dfrain_drop
[out]:
date store_nbr item_nbr units tavg preciptotal
2012-01-01 1 2 2 42 0.05
2012-01-01 2 3 20 62 0.15
2012-01-01 2 4 1 62 0.15
2012-01-01 3 3 29 72 1.05
...(New Day)
date store_nbr item_nbr units tavg preciptotal
2012-01-02 1 2 0 47 0.10
2012-01-02 2 3 5 60 0.0
2012-01-02 2 4 0 60 0.0
2012-01-02 3 3 0 67 .23
那實際上並不是我想要的。我想保留不在我的字典中的值。 – Hound
你的OP說「刪除所有不包含item_nbr作爲我的字典值」的行。 「如果不包含則刪除」是「如果包含則保留」;這也符合您的樣本結果。無論如何,你可以通過在'query'中使用'not in'而不是'in'來翻轉你所保留的內容。 – cphlewis
我看到你編輯了帖子,但它仍然與上面的評論不一致(並且商店3只保留了item_nbr 3行中的一個)。 – cphlewis