Create dataframe from lists
Tags: #python #list #dataframe #snippet #pandas
Author: Florent Ravenel
Here below you will find how to create a pandas DataFrame from two lists in Python.

Input

Import libraries

1
import pandas as pd
Copied!

Variables

1
# Setup your columns name
2
col_key = "KEYS"
3
col_value = "VALUE"
Copied!

Lists

1
keys = [1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012]
2
value = [219, 146, 112, 127, 124, 180, 236, 207, 236, 263, 350, 430, 474, 526, 488, 537, 500, 439]
Copied!

Model

Create zip iterator

1
# Call zip(iter1, iter2) with one list as iter1 and another list as iter2 to create a zip iterator containing pairs of elements from the two lists.
2
zip_iterators = zip(keys, value)
Copied!

Output

Create dataframe

1
df = pd.DataFrame(zip_iterators, columns=[col_key, col_value])
2
df
Copied!
1
Copied!
Copy link
Edit on GitHub