Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有有效的方法用python编写此代码_Python_Python 3.x_Pandas_List_Numpy - Fatal编程技术网

有没有有效的方法用python编写此代码

有没有有效的方法用python编写此代码,python,python-3.x,pandas,list,numpy,Python,Python 3.x,Pandas,List,Numpy,我想用python编写这段代码 proc sql; select count(distinct ID_1) from DATA where ID_1 = ID_2 and ID_type in ("11","23","46"); quit; 我可以分三步来做 a = [x if x==y and z in ("11","23", "46") for x,y,z in zip(DATA['x'],DATA['y'],DATA['z'])] a = [i for i in a if str(i)

我想用python编写这段代码

proc sql;
select count(distinct ID_1)
from DATA
where ID_1 = ID_2 and ID_type in ("11","23","46");
quit;
我可以分三步来做

a = [x if x==y and z in ("11","23", "46") for x,y,z in zip(DATA['x'],DATA['y'],DATA['z'])]
a = [i for i in a if str(i) != 'nan']
len(np.unique(a))

有没有有效的方法来编写相同的代码。

大多数常见的SQL操作都可以轻松地用python和Panda进行翻译:

DATA[(DATA.ID_1 == DATA.ID_2) & (DATA.ID_type.isin(["11", "23", "46"]))].ID_1.nunique()

有关更多信息,请阅读。

使用
query
方法进行不同的筛选:

DATA.query('ID_1 == ID_2 and ID_type.isin(["11", "23", "46"])').ID_1.nunique()

我不完全确定你是否可以使用
isin
query
中使用。也许可以改为使用中的
?您可以在查询中使用一些系列方法,例如
isin
isnull()
不是很好吗?哦,太酷了。我知道isnull,但不确定isin。谢谢,它显示错误类型错误:不可损坏类型:“numpy.ndarray”