W3docs

Creating an empty Pandas DataFrame, and then filling it

Note that in above example, the DataFrame is created empty first, and then columns are added one by one using the assignment operator (=).

Creating an empty Pandas DataFrame, and then filling it in Python

import pandas as pd

# create an empty DataFrame
df = pd.DataFrame()

# add data to the DataFrame
df['name'] = ['John', 'Jane', 'Bob']
df['age'] = [30, 25, 35]
df['city'] = ['New York', 'Los Angeles', 'Chicago']

print(df)

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

Note that in above example, the DataFrame is created empty first, and then columns are added one by one using the assignment operator (=). The column names are specified as strings inside square brackets ([]). The column data is added as a list.