In this article we will discuss different ways to create an empty DataFrame and then fill data in it later by either adding rows or columns.

Suppose we want to create an empty DataFrame first and then append data into it at later stages. Let’s see how to do that,

Import python’s pandas module like this,

import pandas as pd

Create an empty DataFrame with only column names but no rows

Suppose we know the column names of our DataFrame but we don’t have any data as of now. So we will create an empty DataFrame with only column names like this,

# Creating an empty Dataframe with column names only
dfObj = pd.DataFrame(columns=['User_ID', 'UserName', 'Action'])

print("Empty Dataframe ", dfObj, sep='\n')

Contents of the created empty DataFrame will be,
Columns: [User_ID, UserName, Action]
Index: []

How it worked ?

Dataframe class provides a constructor to create Dataframe object by passing column names , index names & data in argument like this,

def __init__(self, data=None, index=None, columns=None, dtype=None,

To create an empty dataframe object we passed columns argument only and for index & data default arguments will be used.

Append rows to empty DataFrame

As we have created an empty DataFrame, so let’s see how to add rows to it,

# Append rows in Empty Dataframe by adding dictionaries
dfObj = dfObj.append({'User_ID': 23, 'UserName': 'Riti', 'Action': 'Login'}, ignore_index=True)
dfObj = dfObj.append({'User_ID': 24, 'UserName': 'Aadi', 'Action': 'Logout'}, ignore_index=True)
dfObj = dfObj.append({'User_ID': 25, 'UserName': 'Jack', 'Action': 'Login'}, ignore_index=True)

print("Dataframe Contens ", dfObj, sep='\n')

Output:
  User_ID UserName  Action
0      23     Riti   Login
1      24     Aadi  Logout
2      25     Jack   Login

Three rows were added to the DataFrame.

Create an complete empty DataFrame without any column name or indices

We can create a complete empty dataframe by just calling the Dataframe class constructor without any arguments like this,

# Create an completely empty Dataframe without any column names, indices or data
dfObj = pd.DataFrame()

As we have not passed any arguments, so default value of all arguments will be None and it will create an empty dataframe dfObj. It’s contents are as follows,
Columns: []
Index: []

Now let’s see how to append columns with data to this empty Dataframe,

Appends columns to an empty DataFrame

# Append columns to the Empty DataFrame
dfObj['UserName'] = ['Riti', 'Aadi', 'Jack']
dfObj['Name'] = ['Riti', 'Aadi', 'Jack']
dfObj['Name'] = ['Riti', 'Aadi', 'Jack']

print("Dataframe Contents ", dfObj, sep='\n')

Output:
Dataframe Contens 
  UserName  Name
0     Riti  Riti
1     Aadi  Aadi
2     Jack  Jack

Create an empty Dataframe with column names & row indices but no data

It might be possible in some cases that we know the column names & row indices at start but we don’t have data yet. So we will create an empty DataFrame and add data to it at later stages like this,

# Create an empty Dataframe with columns or indices
dfObj = pd.DataFrame(columns=['User_ID', 'UserName', 'Action'], index=['a', 'b', 'c'])

print("Empty Dataframe", dfObj, sep='\n')

Here we passed the columns & index arguments to Dataframe constructor but without data argument. So, it will create an empty dataframe with all data as NaN.
  User_ID UserName Action
a     NaN      NaN    NaN
b     NaN      NaN    NaN
c     NaN      NaN    NaN

Add rows to an empty dataframe at existing index

dfObj.loc['a'] = [23, 'Riti', 'Login']
dfObj.loc['b'] = [24, 'Aadi', 'Logout']
dfObj.loc['c'] = [25, 'Jack', 'Login']

print("Dataframe Contents ", dfObj, sep='\n')

output:
Dataframe Contents 
  User_ID UserName  Action
a      23     Riti   Login
b      24     Aadi  Logout
c      25     Jack   Login

Complete example is as follows:
import pandas as pd


def main():
    print('*** Create an empty DataFrame with only column names ***')

    # Creating an empty Dataframe with column names only
    dfObj = pd.DataFrame(columns=['User_ID', 'UserName', 'Action'])

    print("Empty Dataframe ", dfObj, sep='\n')

    print('*** Appends rows to an empty DataFrame using dictionary with default index***')

    # Append rows in Empty Dataframe by adding dictionaries
    dfObj = dfObj.append({'User_ID': 23, 'UserName': 'Riti', 'Action': 'Login'}, ignore_index=True)
    dfObj = dfObj.append({'User_ID': 24, 'UserName': 'Aadi', 'Action': 'Logout'}, ignore_index=True)
    dfObj = dfObj.append({'User_ID': 25, 'UserName': 'Jack', 'Action': 'Login'}, ignore_index=True)

    print("Dataframe Contens ", dfObj, sep='\n')

    print('*** Create an completely empty DataFrame ***')

    # Create an completely empty Dataframe without any column names, indices or data
    dfObj = pd.DataFrame()

    print("Empty Dataframe", dfObj, sep='\n')

    print('*** Appends columns to an empty DataFrame ***')

    # Append columns to the Empty DataFrame
    dfObj['UserName'] = ['Riti', 'Aadi', 'Jack']
    dfObj['Name'] = ['Riti', 'Aadi', 'Jack']
    dfObj['Name'] = ['Riti', 'Aadi', 'Jack']

    print("Dataframe Contents ", dfObj, sep='\n')

    print('*** Create an empty DataFrame with column and index names but no Data ***')

    # Create an empty Dataframe with columns or indices
    dfObj = pd.DataFrame(columns=['User_ID', 'UserName', 'Action'], index=['a', 'b', 'c'])

    print("Empty Dataframe", dfObj, sep='\n')

    print('*** Appends rows to an empty DataFrame on an existing index***')

    dfObj.loc['a'] = [23, 'Riti', 'Login']
    dfObj.loc['b'] = [24, 'Aadi', 'Logout']
    dfObj.loc['c'] = [25, 'Jack', 'Login']

    print("Dataframe Contents ", dfObj, sep='\n')


if __name__ == '__main__':
    main()

Output:
*** Create an empty DataFrame with only column names ***
Empty Dataframe 
Empty DataFrame
Columns: [User_ID, UserName, Action]
Index: []
*** Appends rows to an empty DataFrame using dictionary with default index***
Dataframe Contens 
  User_ID UserName  Action
0      23     Riti   Login
1      24     Aadi  Logout
2      25     Jack   Login
*** Create an completely empty DataFrame ***
Empty Dataframe
Empty DataFrame
Columns: []
Index: []
*** Appends columns to an empty DataFrame ***
Dataframe Contents 
  UserName  Name
0     Riti  Riti
1     Aadi  Aadi
2     Jack  Jack
*** Create an empty DataFrame with column and index names but no Data ***
Empty Dataframe
  User_ID UserName Action
a     NaN      NaN    NaN
b     NaN      NaN    NaN
c     NaN      NaN    NaN
*** Appends rows to an empty DataFrame on an existing index***
Dataframe Contents 
  User_ID UserName  Action
a      23     Riti   Login
b      24     Aadi  Logout
c      25     Jack   Login

 

 

Subscribe with us to join a list of 2000+ Programmers for weekly newsletter.