[英]Is there any efficient way to write this code in python
I want to write this code in python.我想在 python 中编写这段代码。
proc sql;
select count(distinct ID_1)
from DATA
where ID_1 = ID_2 and ID_type in ("11","23","46");
quit;
I can do this in three steps我可以通过三个步骤做到这一点
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))
Is there any efficient way to write the same code.是否有任何有效的方法来编写相同的代码。
Most common SQL operations can be easily translated in python and pandas:最常见的 SQL 操作可以在 python 和 pandas 中轻松转换:
DATA[(DATA.ID_1 == DATA.ID_2) & (DATA.ID_type.isin(["11", "23", "46"]))].ID_1.nunique()
Read the introduction to pandas for more.阅读pandas 的介绍了解更多信息。
A different take filtering using query
method:使用query
方法进行不同的过滤:
DATA.query('ID_1 == ID_2 and ID_type.isin(["11", "23", "46"])').ID_1.nunique()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.