How to create a data frame from scratch (in Python)
Task
Sometimes it is useful to create a small table of data directly in code, without first needing to store the data in a file and load it from there. This can be useful for creating small tables for testing purposes, or for creating small lookup tables that hold abbreviations, IDs, etc. What’s the easiest way to build such a table?
Solution
In pandas, the pd.DataFrame
function can construct new DataFrames. Just provide it with a dictionary whose keys are the column headers and whose values are the column contents.
Here’s an example:
1
2
3
4
5
6
7
import pandas as pd
df = pd.DataFrame( {
'Last name' : [ 'Potter', 'Weasley', 'Granger', 'Malfoy' ],
'First name' : [ 'Harry', 'Ron', 'Hermione', 'Draco' ],
'House' : [ 'Gryffindor', 'Gryffindor', 'Gryffindor', 'Slytherin' ]
} )
df
Last name | First name | House | |
---|---|---|---|
0 | Potter | Harry | Gryffindor |
1 | Weasley | Ron | Gryffindor |
2 | Granger | Hermione | Gryffindor |
3 | Malfoy | Draco | Slytherin |
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Contributed by Nathan Carter (ncarter@bentley.edu)