This community-built FAQ covers the “Create a DataFrame I” exercise from the lesson “Creating, Loading, and Selecting Data with Pandas”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Data Science
Data Analysis with Pandas
FAQs on the exercise Create a DataFrame I
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Need broader help or resources? Head here.
Looking for motivation to keep learning? Join our wider discussions.
Learn more about how to use this guide.
Found a bug? Report it!
Have a question about your account or billing? Reach out to our customer support team!
None of the above? Find out where to ask other questions here!
In this exercise, the output table prints color as the first column after index, however Product ID was the first column entered in the code. How does python decide how to arrange the columns?
1 Like
I don’t get what do they mean by “the columns should be of the same size otherwise you will get an error”.
If you input the following dictionary then it will throw an error because the number of columns are not matching. First and second keys have a values list containing 4 elements while in the third key the value is a list containing only 3 elements so this dictionary is incompatible for pandas dataframe input. Hope it helps…
{
'Product ID': [1, 2, 3, 4],
"Product Name": ["t-shirt", "t-shirt", "skirt", "skirt"],
"Color": ["blue", "green", "red"]
# add Product Name and Color here
}
1 Like
Pandas will list the columns in Alphabetical order
Hi Everyone
Newbie here who came across some interesting reading in Python release notes and StackOverflow forums:
As of Python 3.7, Python Dictionary order is guaranteed to be insertion order.
In lesson: Create a DataFrame I:
The lesson teaches us that when you create a Dataframe from a Dictionary, Pandas will reorder the columns in alphabetical order.
However, when I copy the code and test it off platform using Python 3.10, the original Dictionary column order is preserved and not re-arranged as expected in the lesson.
Can this be used safely or is there something I am not aware of that could cause instability or issues down the road?
Do you think the lesson should be updated to reflect the changes in Dictionary behavior assuming this can be used safely?
Please see URL for lesson below as well as test code that should re-order the columns in alphabetical order:
URL:
https://www.codecademy.com/paths/analyze-data-with-python/tracks/ida-4-data-manipulation-pandas/modules/ida-4-1-introduction-to-pandas/lessons/pandas-i/exercises/create-dataframe-i
Test Code:
import pandas library:
import pandas as pd
create Pandas Dataframe from a Dictionary
df1 = pd.DataFrame({
'name': ['John Smith', 'Jane Doe', 'Joe Schmo'],
'address': ['123 Main St.', '456 Maple Ave.', '789 Broadway'],
'age': [34, 28, 51]
})
prints Pandas Dataframe created from Dictionary - expected output should be a table with columns in alphabetical order but instead retains original Dict order in Python 3.10
print(df1)