Creating an empty Pandas DataFrame, and then filling it
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)
Watch a video course
Python - The Practical Guide
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.