Turning this all into a DataFrame
You've zipped lists together, created a function to house your code, and even used the function in a list comprehension to generate a list of dictionaries. That was a lot of work and you did a great job!
You will now use all of these to convert the list of dictionaries into a pandas DataFrame. You will see how convenient it is to generate a DataFrame from dictionaries with the DataFrame() function from the pandas package.
The lists2dict() function, feature_names list, and row_lists list have been preloaded for this exercise.
Go for it!
Deze oefening maakt deel uit van de cursus
Python Toolbox
Oefeninstructies
- To use the
DataFrame()function you need, first import the pandas package with the aliaspd. - Create a DataFrame from the list of dictionaries in
list_of_dictsby callingpd.DataFrame(). Assign the resulting DataFrame todf. - Inspect the contents of
dfprinting the head of the DataFrame. Head of the DataFramedfcan be accessed by callingdf.head().
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# Import the pandas package
# Turn list of lists into list of dicts: list_of_dicts
list_of_dicts = [lists2dict(feature_names, sublist) for sublist in row_lists]
# Turn list of dicts into a DataFrame: df
df = ____
# Print the head of the DataFrame