Connecting to SQL Server using Python

This article will explain how to connect to SQL server, issue a Query and create a DataFrame using Pandas, My example queries a SQL server Database with the purpose of retreving the number of bookings that a small BnB made over a period of 3 years.

Connect to SQL code:

This returned the following sample.

next bit is to make the data look more presentable.

issuing all_bookings results in the following  sampleoutput.

you can see from the above output that in August 2017 we had 2 bookings but in August 2018 we had 3.

 

We can also pivot the results a lot easier using Python than using SQL.

results in the sample output, where NaN means no bookings.

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *