Python create an empty dataframe – Pandas: How to create an empty DataFrame and append rows & columns to it in python

Methods to create an empty data frame and append rows and column to it

Create an empty dataframe in python: In this article, we discuss a dataframe how we can create an empty dataframe and after creating an empty dataframe how can we append rows and columns in it.

Before understanding this concept let us understand some basic concepts and terminologies.

Dataframe

Python create an empty dataframe: Dataframe is a 2D data structure in python that store or represent the data in the 2D form or simply say in tabular form. The tabular form consists of rows, columns, and actual data. To create a dataframe or to use the dataframe we have to import the pandas package in our program.

As we cannot use dataframe without pandas let see what pandas in python are.

Pandas

Pandas make empty dataframe: Pandas is a package in python that is used to analyze data in a very easy way. The reason why pandas is so famous is that it is very easy to use. But we can not directly use the pandas package in our program. To use this package first we have to import it.

DataFrame()

Pandas declare empty dataframe: This is the method that is widely used in this article. Let us take a brief about this method.DataFrame() is a constructor that is used to create dataframes in pandas.

Syntax: pandas.DataFrame(data=Noneindex=Nonecolumns=Nonedtype=Nonecopy=False)

Note:As we import pandas as pd in our program so we use pd.DataFrame() instead of pandas.DataFrame().

Now we see some theories and definitions related to pandas and dataframes let us see how we can practically implement it in our program.

In our dataframe definition, we see discuss that dataframe is consists of rows/index, columns, and data. Now think we want an empty dataframe that can be possible in 3 cases. First when there is no row and no column in the dataframe, Second when there is the only column and there when we have both rows and columns but the data value is NAN. Let us see these cases or methods one by one.

  • Method 1-Create an empty dataframe without any column and rows and then append them one by one

How to create an empty dataframe: Let us see this method with the help of an example

import pandas as pd
df=pd.DataFrame()
print(df)

Output

Empty DataFrame
Columns: []
Index: []
Here we see that with the Dataframe() constructor we can easily create our dataframe. But our dataframe is empty as we didn’t pass any argument inside DataFrame() constructor. Now as we create our empty dataframe we can easily add columns and data to it. Let see how we can achieve this with the help of an example.
df['Name']=['Raj','Rahul','Aman']
df['Marks']=[100,98,77]
print(df)

Output

     Name  Marks
0    Raj    100
1  Rahul     98
2   Aman     77

Here Name and Marks are columns of the dataframe. Now, remember dictionary we can access and assign elements in a dictionary using a key similarly we done this task here but the pattern here is different.

  • Method 2-Create a dataframe with only a column and then append rows or indexes in it

How to create empty dataframe: Let us discuss this method with the help of an example.

df=pd.DataFrame(columns=['Name','Marks'])
print(df)

Output

Empty DataFrame
Columns: [Name, Marks]
Index: []

Here we see that we easily create empty dataframe bypassing columns in DataFrame() constructor. Now we have our columns so we can append rows/index in our dataframe using the append() method.

df = df.append({'Name' : 'Raj', 'Marks' : 100}, 
                ignore_index = True)
df = df.append({'Name' : 'Rahul', 'Marks' : 98},
                ignore_index = True)
df = df.append({'Name' : 'Aman', 'Marks' : 77},
               ignore_index = True)
print(df)

Output

     Name Marks
0    Raj   100
1  Rahul    98
2   Aman    77

Here we see if we have information about columns in the dataframe then we can easily add rows and data easily using the append() method. As the append() method does not change the actual dataframe so we assign the value returned by the .append() method in our original dataframe otherwise our dataframe will remain unchanged.

Note: append() method returns a new dataframe object

  • Method 3- Create an empty dataframe with column name and index/rows but no data

Let us see this method with the help of an example.

df=pd.DataFrame(columns=['Name','Marks'],index = [1,2,3])
print(df)

Output

  Name Marks
1  NaN   NaN
2  NaN   NaN
3  NaN   NaN

Here we see that we have created an empty dataframe that have both rows and column by simply passing column and index in DataFrame() constructor. Now we see how we can add data to it.

df.loc[1] = ['Raj', 100]
df.loc[2] = ['Rahul', 98]
df.loc[3] = ['Aman', 77]
print(df)

Output

      Name Marks
1    Raj   100
2  Rahul    98
3   Aman    77

If we have rows and indexes then we can add data in our dataframe using loc. loc is used to access groups of rows and columns by values.

So these are the methods to create an empty dataframe and add rows and columns to it.

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.

Read more Articles on Python Data Analysis Using Padas – Creating Dataframe Objects: