[英]Slice pandas DataFrame where column's value exists in another array
I have a pandas.DataFrame
with a large amount of data. 我有一个包含大量数据的pandas.DataFrame
。 In one column are randomly repeating keys. 在一列中随机重复键。 In another array I have a list of of theys keys for which I would like to slice from the DataFrame
along with the data from the other columns in their row. 在另一个数组中,我有一个我想要从DataFrame
切片的theys键列表以及它们行中其他列的数据。
keys : 钥匙 :
keys = numpy.array([1,5,7])
data : 数据 :
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
I would like slice all rows from the DataFrame
if the value in the column a
matches a value from keys
. 如果列a
中的值与keys
中的值匹配,我想从DataFrame
切片所有行。
Desired result : 期望的结果 :
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
You can use isin
: 你可以使用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]
or query
: 或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.