有没有有效的方法用python编写此代码
我想用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)
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”