How to Create a pandas DataFrame in Python

In this tutorial, you will create a pandas DataFrame from scratch. We also have a tutorial that shows you how to import a CSV file as DataFrame.

TLDR solution

import pandas as pd

data = {'column_a': [a1, a2, a3],
        'column_b': [b1, b2, b3]
        }

df = pd.DataFrame(data)

Step-by-Step Example

Step 1: Install the pandas Package

If you don't have pandas already installed, execute the following command in your terminal:

pip install pandas

Step 2: Create a DataFrame

Let's say, you have the following table as DataFrame:

fishcount
salmon200
pufferfish20
shark2

The following code produces the corresponding DataFrame by first defining a Python dictionary of the columns and then using the DataFrame function to load it:

create_df.py
import pandas as pd

data = {'fish': ['salmon', 'pufferfish', 'shark'],
        'count': [200, 20, 2]
        }

df = pd.DataFrame(data)

print(df)

The output should look like this:

         fish  count
0      salmon    200
1  pufferfish     20
2       shark      2

That's it! You just created a DataFrame from scratch.