Create Pandas DataFrame in Python

Let’s create pandas DataFrame in Python. A Data frame is a two-dimensional data structure containing labeled axes (rows and columns) i.e., data is aligned in a tabular fashion in rows and columns.

pandas.DataFrame

A pandas DataFrame can be created by passing the following parameters:

pandas.DataFrame(data, index, columns, dtype, copy)
Sr.NoParameters Description
1data
input data can be dictionary, series, arrays, constants, list and also another DataFrame.
2index
index (rows label) of the resulting data frame.
3columns
column labels of the resulting data frame, default is RangeIndex (0, 1,…, n) if no column labels are provided.
4type
datatype (dtype) to force on each column. If None then infer.
5copy
copy data from inputs, default is False.

Different Methods to Create Python DataFrame

Let’s explore various ways to create DataFrame using inputs like:

  1. Dictionary
  2. Lists
  3. Series
  4. Numpy arrays
  5. Empty DataFrame

1. Create a DataFrame from Dictionary (Preferred)

nameweight
0Sam70
1Zen85
2Robin55
3John90

2. Create a DataFrame from Lists

The DataFrame can be created easily using a list.

count
01
12
23
34
45

3. Create a DataFrame from Pandas Series

Let’s create DataFrame from dict of series. The resultant indices are the union of all the series of passed indexed.

ab
01.01
12.02
23.03
3NaN4

Note: In ‘a’ series of data only 3 values are passed, therefore missing index is appended with NaN.

4. Create a DataFrame from ndarrays 

Python Numpy array can also be used to create a Pandas DataFrame.

ab
012
134
256

5. Creating an empty dataframe

An Empty Pandas Dataframe in python without any rows and columns can be created just by calling a dataframe constructor.

5.1 Empty DataFrame

Empty DataFrame
Columns: []
Index: []

5.2 Empty DataFrame with Column Names and Rows indices

Similarly, we can create an empty data frame with only columns, rows or both. In the example below, we will create an empty DataFrame with columns: name, age, weight, and 3 rows.

nameageweight
0NaNNaNNaN
1NaNNaNNaN
2NaNNaNNaN

For more reference feel free to explore Pandas DataFrame documentation.

Start with Machine Learning Tutorial here.

Leave a Comment