I am new with Python and have a problem that I don't know how to solve.
I have a dataframe (localdf["where_condition"]) which contains the following values:
0 FirstName ='sonali'
1 Gender='F'
2 NaN
function:
def filter_record_count_check(localdf):
try:
sql = 'select count(*) as CNT from ' + localdf["schema_table"] + ' where ' + localdf["where_condition"]
sql = sql.to_dict()
print(sql)
df_list=[]
for i in sql.values():
df = pd.read_sql_query(i, db_connection)
df_list.append(df.CNT[0])
print("df_list")
print(df_list)
return df_list
sql output:
{0: "select count(*) as CNT from testdb.DimCurrency where FirstName ='sonali'", 1: "select count(*) as CNT from testdb.banking_fraud where Gender='F'", 2: nan}
df_list output:
[2, 208]
issue is when any null value/nan coming in sql whole output coming as nan when calling same function in another function. sample output
table_name column_name ... record_count **filter_column_cnt**
0 DimCurrency PersonID ... 6 NaN
1 banking_fraud NaN ... 1000 NaN
2 banking_fraud NaN ... 1000 NaN
expected output is :
table_name column_name ... record_count **filter_column_cnt**
0 DimCurrency PersonID ... 6 2
1 banking_fraud NaN ... 1000 208
2 banking_fraud NaN ... 1000 NaN
NaNor a string? Withdata != 'NaN'you make it seem like it's a string. (2) What does "if any nan in dataframe then my query will not run else run" mean? Do you mean if there's at least oneNaNin the column then none of the queries should run (even if they could be build)?dictheresql = sql.to_dict(), when you're only using its values?df.head().to_dict()is a good one here. 2) Focus just on the problem at hand: Filtering NaNs out of a pandas dataframe column. Make sure you get that part right first before trying to do anything else with your data.