Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify na_values in pandas using read_sql?

Tags:

python

pandas

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?

like image 673
Feynman27 Avatar asked Jun 17 '16 21:06

Feynman27


People also ask

What does PD Read_sql do?

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).

Which method is used in pandas to detect null values?

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.


1 Answers

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])
like image 74
MaxU - stop WAR against UA Avatar answered Oct 11 '22 13:10

MaxU - stop WAR against UA