![](/img/trans.png)
[英]Python pandas dataframe: delete rows where value in column exists in another
[英]Slice pandas DataFrame where column's value exists in another array
我有一個包含大量數據的pandas.DataFrame
。 在一列中隨機重復鍵。 在另一個數組中,我有一個我想要從DataFrame
切片的theys鍵列表以及它們行中其他列的數據。
鑰匙 :
keys = numpy.array([1,5,7])
數據 :
indx a b c d
0 5 25.0 42.1 13
1 2 31.7 13.2 1
2 9 16.5 0.2 9
3 7 43.1 11.0 10
4 1 11.2 31.6 10
5 5 15.6 2.8 11
6 7 14.2 19.0 4
如果列a
中的值與keys
中的值匹配,我想從DataFrame
切片所有行。
期望的結果 :
indx a b c d
0 5 25.0 42.1 13
3 7 43.1 11.0 10
4 1 11.2 31.6 10
5 5 15.6 2.8 11
6 7 14.2 19.0 4
你可以使用isin
:
>>> df[df.a.isin(keys)]
a b c d
indx
0 5 25.0 42.1 13
3 7 43.1 11.0 10
4 1 11.2 31.6 10
5 5 15.6 2.8 11
6 7 14.2 19.0 4
[5 rows x 4 columns]
或query
:
>>> df.query("a in @keys")
a b c d
indx
0 5 25.0 42.1 13
3 7 43.1 11.0 10
4 1 11.2 31.6 10
5 5 15.6 2.8 11
6 7 14.2 19.0 4
[5 rows x 4 columns]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.