How to Create or Initialize a Pandas DataFrame?

Pandas – Create or Initialize DataFrame

In Python Pandas module, DataFrame is a very basic and important type. To create a DataFrame from different sources of data or other Python datatypes, we can use DataFrame() constructor.

In this tutorial, we will learn different ways of how to create and initialize Pandas DataFrame.

Syntax of DataFrame()

The syntax of DataFrame() class is

DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)

where all the arguments are optional and

Examples

1. Create an empty DataFrame

To create an empty DataFrame, pass no arguments to pandas.DataFrame() class.

In this example, we create an empty DataFrame and print it to the console output.

Python Program

import pandas as pd

df = pd.DataFrame()
print(df)
Run Code

Output

Empty DataFrame
Columns: []
Index: []

As we have provided no arguments, the columns array is empty and index array is empty.

2. Create DataFrame from List of Lists

To initialize a DataFrame from list of lists, you can pass this list of lists to pandas.DataFrame() constructor as data argument.

In this example, we will create a DataFrame for list of lists. Each inner list represents a row in DataFrame.

Python Program

import pandas as pd

# List of lists
data = [['a1', 'b1', 'c1'],
        ['a2', 'b2', 'c2'],
        ['a3', 'b3', 'c3']]

df = pd.DataFrame(data)
print(df)
Run Code

Output

    0   1   2
0  a1  b1  c1
1  a2  b2  c2
2  a3  b3  c3

3. Create DataFrame from Dictionary

To initialize a DataFrame from dictionary, pass this dictionary to pandas.DataFrame() constructor as data argument.

In this example, we will create a DataFrame for a dictionary. Each key:value pair in the dictionary represents column_name:column_data in the DataFrame.

Python Program

import pandas as pd

# List of lists
data = {'col1': ['a1', 'a2', 'a3'],
        'col2': ['b1', 'b2', 'b3'],
        'col3': ['c1', 'c2', 'c3']}

df = pd.DataFrame(data)
print(df)
Run Code

Output

  col1 col2 col3
0   a1   b1   c1
1   a2   b2   c2
2   a3   b3   c3

Summary

In this Pandas Tutorial, we learned how to create an empty DataFrame, and then to create a DataFrame with data from different Python objects, with the help of well detailed examples.

Related Tutorials

Privacy Policy Terms of Use

SitemapContact Us