Creating DataFrames with the Pandas Constructor

We’ll explore the Pandas DataFrame constructor and how to use it to build DataFrames from scratch.

Creating a DataFrame from Lists or NumPy Arrays

You can create a DataFrame by passing a list or NumPy array of data to the constructor. Each list or array represents a column in the DataFrame.

data = {'Column1': [1, 2, 3],
df = pd.DataFrame(data)

Specifying an Index

By default, Pandas assigns a numeric index to rows. You can specify a custom index for your DataFrame using the index parameter:

custom_index = ['Row1', 'Row2', 'Row3']
df = pd.DataFrame(data, index=custom_index)

Creating a DataFrame from a Dictionary of Series or Lists

You can also create a DataFrame from a dictionary of Series or lists. Each dictionary key becomes a column label, and the corresponding Series or list becomes the column’s data.

data = {'Column1': pd.Series([1, 2, 3]),
df = pd.DataFrame(data)

Creating an Empty DataFrame

To create an empty DataFrame, simply call the constructor without any data. You can then add data later using DataFrame methods.

empty_df = pd.DataFrame()

Creating DataFrames with Different Data Types

DataFrames can contain columns with different data types (integers, strings, floats, etc.). Pandas will infer the data types for each column.

data = {'IntColumn': [1, 2, 3],
df = pd.DataFrame(data)

Leave a Reply