Creating Pandas DataFrames from Lists and Dictionaries
Posted by Chris Moffitt in articles
Whenever I am doing analysis with pandas my first goal is to get data into
a panda’s DataFrame using one of the many available options. For the vast
majority of instances, I use read_excel
, read_csv
, or read_sql
.
However, there are instances when I just have a few lines of data or some calculations
that I want to include in my analysis. In these cases it is helpful to
know how to create DataFrames from standard python data structures such as lists or
dictionaries. The basic process is not difficult but because there are several different
options it is helpful to understand how each works. I can never remember whether
I should use from_dict
, from_records
, from_items
or the
default DataFrame
constructor. Normally, through some trial and error,
I figure it out. Since it is still confusing to me, I thought I would walk through
several examples below to clarify the different approaches. At the end of the article,
I briefly show how this can be useful when generating Excel reports.