Lesson 06
Creating a DataFrame
Build a DataFrame from a dict of columns, a list of rows, a list of dicts, or a dict of Series, and set the index and column order yourself.
A DataFrame is a table: a row index, named columns, and the values. You can build one from a dict where each key is a column, or from rows where each row is a list, a tuple, or a dict.
From a dict of columns
This is the usual way. Each key is a column name and each value is the column data. All the lists must be the same length.
import pandas as pd
team = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey"],
"hours": [42, 31, 55],
"score": [88, 74, 91],
})
print(team)
print(team.columns.tolist())Output:
name hours score
0 Avery 42 88
1 Jordan 31 74
2 Casey 55 91
['name', 'hours', 'score']From rows
A list of tuples or lists is one row each. Pass columns to name them, or you get 0, 1, 2 as headings. index names the rows.
import pandas as pd
rows = [("Avery", 42, 88), ("Jordan", 31, 74), ("Casey", 55, 91)]
team = pd.DataFrame(rows, columns=["name", "hours", "score"], index=["a", "b", "c"])
print(team)
print(pd.DataFrame(rows))Output:
name hours score
a Avery 42 88
b Jordan 31 74
c Casey 55 91
0 1 2
0 Avery 42 88
1 Jordan 31 74
2 Casey 55 91From a list of dicts
Each dict is one row and its keys are the column names. A key that is missing from one row becomes NaN, which makes this shape forgiving when records differ.
import pandas as pd
records = [
{"name": "Avery", "hours": 42, "score": 88},
{"name": "Jordan", "hours": 31},
{"name": "Casey", "hours": 55, "score": 91, "city": "Northgate"},
]
print(pd.DataFrame(records))Output:
name hours score city
0 Avery 42 88.0 NaN
1 Jordan 31 NaN NaN
2 Casey 55 91.0 NorthgateFrom a dict of Series
When the values are Series, pandas lines the columns up on their labels rather than on their order. Any label that is missing from a column is filled with NaN.
import pandas as pd
hours = pd.Series({"Avery": 42, "Jordan": 31, "Casey": 55})
score = pd.Series({"Casey": 91, "Avery": 88})
print(pd.DataFrame({"hours": hours, "score": score}))Output:
hours score
Avery 42 88.0
Casey 55 91.0
Jordan 31 NaNChoose the index and the columns
columns can also pick and reorder. set_index promotes a column to the row index, and reset_index puts it back as a normal column.
import pandas as pd
team = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey"],
"hours": [42, 31, 55],
"score": [88, 74, 91],
})
print(team[["score", "name"]])
by_name = team.set_index("name")
print(by_name)
print(by_name.reset_index())Output:
score name
0 88 Avery
1 74 Jordan
2 91 Casey
hours score
name
Avery 42 88
Jordan 31 74
Casey 55 91
name hours score
0 Avery 42 88
1 Jordan 31 74
2 Casey 55 91Key takeaways
-
A dict of lists is one column per key
-
A list of tuples is one row each, and
columnsnames them -
A list of dicts fills any missing key with
NaN -
A dict of Series lines up on labels, not on order
-
Next: DataFrame Attributes
-
Previous: Series Statistics
Loading playground…
Check your understanding
Creating a DataFrame Quiz
In pd.DataFrame({"a": [1, 2]}), what is "a"?
1 / 10