• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Pandas DataFrame / pandas data frame from list / How to Create a Pandas Dataframe from Lists

How to Create a Pandas Dataframe from Lists

January 10, 2018 by cmdlinetips

NumPy is fantastic for numerical data. One can really do powerful operations with numerical data easily and much faster. However, if your data is of mixed type, like some columns are strings while the others are numeric, using data frame with Pandas is the best option.

How to Create Pandas Dataframe from lists?

Let us say we have two lists, one of them is of string type and the other is of type int. We want to make a dataframe with these lists as columns.

>months = ['Jan','Apr','Mar','June']
>days = [31,30,31,30]

We will see three ways to get dataframe from lists.

1. Create pandas dataframe from lists using dictionary

One approach to create pandas dataframe from one or more lists is to create a dictionary first. Let us make a dictionary with two lists such that names as keys and the lists as values.

>d = {'Month':months,'Day':days}
>d
{'Day': [31, 30, 31, 30], 'Month': ['Jan', 'Apr', 'Mar', 'June']}

Here d is our dictionary with names “Day” and “Month” as keys.

# Load pandas as pd
>import pandas as pd

Let us create a pandas dataframe from using pd.DataFrame function with our dictionary as input.

>df = pd.DataFrame(d)
>df
   Day Month
0    31   Jan
1    30   Apr
2    31   Mar
3    30  June

Now we have our pandas dataframe from lists. Notice that the columns of the dataframe is Day first and Month next. Let us say we want Month first and Day next in the dataframe. To specify the order of the columns, we can use “columns” option with pd.DataFrame like

>df = pd.DataFrame(d, columns=['Month','Day'])
>df
  Month  Day
0   Jan    31
1   Apr    30
2   Mar    31
3  June    30

2. Create pandas dataframe from lists using zip

Second way to make pandas dataframe from lists is to use the zip function. We can use the zip function to merge these two lists first. In Python 3, zip function creates a zip object, which is a generator and we can use it to produce one item at a time. To get a list of tuples, we can use list() and create a list of tuples. For this example, we can create a list of tuples like

# Python 3 to get list of tuples from two lists
data_tuples = list(zip(Month,Days))
data_tuples
[('Jan', 31), ('Apr', 30), ('Mar', 31), ('June', 30)]

Note that if you use Python 2, zip(Month,Days) alone is enough to get list of tuples. We don’t need to use list(zip()).

Converting list of tuples to pandas dataframe

We can simply use pd.DataFrame on this list of tuples to get a pandas dataframe. And we can also specify column names with the list of tuples.

>pd.DataFrame(data_tuples, columns=['Month','Day'])
 Month Day
0 Jan 31
1 Apr 30
2 Mar 31
3 June 30

3. Create pandas dataframe from scratch

The third way to make a pandas dataframe from multiple lists is to start from scratch and add columns manually. We will first create an empty pandas dataframe and then add columns to it.

Create Empty Pandas Dataframe

# create empty data frame in pandas
>df = pd.DataFrame()

Add the first column to the empty dataframe.

# add a coumn
>df['Month']  = months
 Month
0   Jan
1   Apr
2   Mar
3  June

Now add the second column.

# add second column
>df['Day'] = days
Month Day
0 Jan 31
1 Apr 30
2 Mar 31
3 June 30

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Create a Pandas Data Frame From Lists? Pandas Convert Two Columns to a DictionaryHow To Convert Pandas Dataframe to a Dictionary Default ThumbnailHow to Split Text in a Column in Data Frame in R? Default ThumbnailHow to Collapse Multiple Columns in Pandas? Groupby with Dictionary

Filed Under: pandas data frame from list, Pandas DataFrame Tagged With: Create DataFrame From Lists In Python, create pandas dataframe, Pandas Dataframe, pandas dataframe from lists, Pandas Tips

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version