SQLAlchemy and pandas for visualization
1. SQLAlchemy and pandas for visualization
Let's take a breather for a second, and look at how we might use SQLAlchemy in a data science context with pandas.2. SQLAlchemy and pandas
A SQLAlchemy ResultSet can be directly supplied to a DataFrame; however, we need to supply the column names separately. Let's experiment with this.3. DataFrame example
We can import pandas as pd which is a common convention. Then we are going to create a DataFrame and supply it a SQLAlchemy ResultSet. Next, we set the DataFrame's columns to the keys in our first result. Finally, we can print the DataFrame to validate that we got the result we desired. From this point, I could do anything I would normally do with a Pandas DataFrame such as creating a figure and visualizing the results.4. Graphing
There is nothing special about a DataFrame created from a ResultSet, so we can graph it the same way we would any DataFrame.5. Graphing example
For example, We can import the pyplot module from matplotlib. Next, if we wanted make a horizontal bar chart of population count by age. We can do that with the plot and barh methods on the DataFrame. In this case, I'm limiting the data to just 10 rows from the DataFrame. Finally, we instruct matplotlib to display the plot. We just did a simple graph here, but you can tweak the graphs to be as fancy as you like with matplotlib!6. Let's practice!
It's your turn to play.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.