pysql = lambda q: pdsql.sqldf(q, globals())
str1 = "select coalesce(ID1, H_ID, [Alternate Source Unique
Identifier]) as Master_ID, [Alternate Source Unique Identifier] as Q_ID
from crosswalk;"
with Timer("Load master_ids:"):
master_id_list = pysql(str1)
print("Records: {}".format(len(master_id_list)))
master_id_list.head()
pySQL只需5秒就可以运行!!
我想在python中编写第二个脚本,因为我不能使用pysql :(,任何想法?请在python中对第二个脚本进行最佳翻译吗?
我已经完成了2个命题,但在时间上并没有真正有效 没有Pandas的第一个(这是必需的)
def coalesce (df, column_names):
i=iter(column_names)
column_name=next(i)
answer=df[colum_name]
for column_name in i:
answer = answer.fillna(df[column_name])
return answer
coalesce(df, ['first', 'third', 'second'])
感谢您的建议!
答案 0 :(得分:0)
如果您只需要计算记录数(len(master_id_list)
),您可以直接使用它:
crosswalk['ID'].size
或
crosswalk.shape
如果您正在寻找唯一值,请尝试:
crosswalk['ID'].unique().size