ResultsSets and pandas DataFrames
We can feed a ResultSet directly into a pandas DataFrame, which is the workhorse of many Data Scientists in PythonLand. Jason demonstrated this in the video. In this exercise, you'll follow exactly the same approach to convert a ResultSet into a DataFrame.
This is a part of the course
“Introduction to Databases in Python”
Exercise instructions
- Import
pandas
aspd
. - Create a DataFrame
df
usingpd.DataFrame()
on the ResultSetresults
. - Set the columns of the DataFrame
df.columns
to be the columns from the first result objectresults[0].keys()
. - Print the DataFrame.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# import pandas
# Create a DataFrame from the results: df
df = ____
# Set column names
df.columns = ____
# Print the DataFrame
print(____)
This exercise is part of the course
Introduction to Databases in Python
In this course, you'll learn the basics of relational databases and how to interact with them.
In this chapter, you will build on your database knowledge by writing more nuanced queries that allow you to filter, order, and count your data—all within the Pythonic framework provided by SQLAlchemy.
Exercise 1: Filtering and targeting dataExercise 2: Connecting to a PostgreSQL databaseExercise 3: Filter data selected from a Table - SimpleExercise 4: Filter data selected from a Table - ExpressionsExercise 5: Filter data selected from a Table - AdvancedExercise 6: Ordering query resultsExercise 7: Ordering by a single columnExercise 8: Ordering in descending order by a single columnExercise 9: Ordering by multiple columnsExercise 10: Counting, summing, and grouping dataExercise 11: Counting distinct dataExercise 12: Count of records by stateExercise 13: Determining the population sum by stateExercise 14: SQLAlchemy and pandas for visualizationExercise 15: ResultsSets and pandas DataFramesExercise 16: From SQLAlchemy results to a plotWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.