在python中条件为null的if-else语句中出现问题



我有以下src_input数据。

2022-09-02 12:25:28.390508,--1,    table_name column_name business_key_name select_column_names  
0  DimCurrency   FirstName                                         
where_condition schema_name        schema_table  
0                      testdb  testdb.DimCurrency  

下面是霉菌代码:

try:
if src_input['column_name'] is not None and src_input['where_condition'].isnull():
#print("test", src_input['where_condition'].isnull())
sql = 'select ' + src_input["column_name"] + ' as src_clmn from ' + 
src_input['schema_table']
sql = sql[0]
df1 = pd.read_sql_query(sql, db_conn)
loging(datetime.datetime.now(), 'df1', df1)
return df1
elif pd.notnull(src_input['column_name']) and pd.notnull(src_input['where_condition']):
sql = 'select ' + src_input["column_name"] + ' as src_clmn from ' + 
src_input['schema_table'] +' where ' + src_input['where_condition']
sql = sql[0]
print(sql)

我的if-else条件不起作用。src_input['where_condition'].isnull()语句输出为true,所以理想情况下,若条件应该运行。不确定我的代码中出了什么问题。

如果我只在if staement中放入src_input['column_name'] is not None,那么我的if条件sql运行良好。

我也试过下面的东西,但没有运气。

pd.notnull(src_input['where_condition'])
src_input['where_condition'] is None
src_input['column_name'] is not None and not src_input['where_condition'].isnull()

问题是,您正在处理一个DataFrame,如果您希望条件正常工作,则需要该数据帧的行。试着做这样的事情:

if not src_input.iloc[0]['column_name'].isna() and src_input.iloc[0]['where_condition'].isnull():
#do something here

使用iloc,你可以选择第一行(你可以迭代每一行,或者制作一个矢量化函数来迭代它们(

我希望它能有所帮助。

最新更新