I'm trying to create a pandas dataframe from a sql table. I read in the data using data=pd.read_sql(query,con=con)
, and this works just fine. However, I wish to set which type of elements in the data frame is NaN. When reading a csv, this can be set using pd.read_csv('file.csv',na_values=['',[]'])
. Is there a similar flag available using read_sql?
read_sql. Read SQL query or database table into a DataFrame. This function is a convenience wrapper around read_sql_table and read_sql_query (for backward compatibility).
In order to check null values in Pandas DataFrame, we use isnull() function this function return dataframe of Boolean values which are True for NaN values.
There is no such parameter because pandas/numpy NaN
corresponds NULL
(in the database), so there is one to one relation.
But if you want to replace other values with NaNs you can do it this way:
df = df.replace(['', 'null'], [np.nan, np.nan])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With