Skip to content
Advertisement

SQL to Dataframe not accounting for empty cells

I pull from a SQL database which sometimes looks like this.

A B C D _ E F _ G

So when I put it into a data frame and print it as a CSV I want it to look like

A, B, C, D, , E, F, , G

But instead it looks like

A, B, C, D, E, F, G

So when I have a full row and then a row with some data missing, if I put it into an excel sheet and deliminate by comma, they do not match up properly.

df5 = pd.DataFrame(SQLQuery)
df5.to_csv(r'finalResults.csv', index = False)

Advertisement

Answer

You should use df5 = pd.read_sql_query(SQLQuery, connectionString)

Then if you do df5.to_csv or .to_excel it will properly format

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement