AppDividend
Latest Code Tutorials

How to Create Empty DataFrame in Pandas

0

To create a DataFrame from different sources of data or other Python data types like list, dictionary, use constructors of DataFrame() class. In this example, we will learn different ways of how to create empty Pandas DataFrame.

How to Create Empty DataFrame

To create empty DataFrame in Pandas, don’t add any row data while constructing new DataFrame, and in return, you will get empty DataFrame.

# app.py

import pandas as pd

dfObj = pd.DataFrame(columns=['ID', 'Name', 'Age'])
print(dfObj, sep='\n')

Output

Empty DataFrame
Columns: [ID, Name, Age]
Index: []

You can see that we got success in creating empty DataFrame. But how? It is because the DataFrame class provides a constructor to create a DataFrame object by passing column names, index names & data in an argument like the following.

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

To create the empty DataFrame object, we passed columns argument only, and for index and data, default arguments will be used.

Create empty DataFrame without any column name or indices

To create an empty DataFrame without any column names or indices, don’t pass any argument while creating a new DataFrame.

# app.py

import pandas as pd

dfObj = pd.DataFrame()
print(dfObj, sep='\n')

Output

Empty DataFrame
Columns: []
Index: []

As we have not passed any function arguments, so the default value of all the arguments will be None, and it will generate the empty DataFrame dfObj.

Appends columns to an empty DataFrame

You can append columns in the empty DataFrame.

# app.py

import pandas as pd

dfObj = pd.DataFrame()
print(dfObj, sep='\n')

dfObj['ID'] = [101, 111, 121]
dfObj['Name'] = ['Katheryn', 'Millie', 'Krunal']
dfObj['Age'] = [28, 15, 21]

print('After appending columns')
print(dfObj)

Output

Empty DataFrame
Columns: []
Index: []
After appending columns
    ID      Name  Age
0  101  Katheryn   28
1  111    Millie   15
2  121    Krunal   21

You can see we added three rows: ID, Name, and Age. 

It does not matter, at which point, we add the data. We can add the data to the DataFrame at any point based on our requirements.

Create empty DataFrame with column and indices

To create an empty DataFrame with column and indices, pass only column names and indices while constructing a new DataFrame.

# app.py

import pandas as pd

dfObj = pd.DataFrame(columns=['ID', 'Name', 'Age'],
                     index=['a', 'b', 'c'])
print(dfObj, sep='\n')

Output

    ID  Name Age
a  NaN  NaN  NaN
b  NaN  NaN  NaN
c  NaN  NaN  NaN

You can see that we created a new DataFrame filled with NaN values.

Append rows to an empty DataFrame at existing index

You can append new data based on the indices.

# app.py

import pandas as pd

dfObj = pd.DataFrame(columns=['ID', 'Name', 'Age'],
                     index=['a', 'b', 'c'])
print(dfObj, sep='\n')

dfObj.loc['a'] = [101, 'Katheryn', 29]
dfObj.loc['b'] = [102, 'Millie', 15]
dfObj.loc['c'] = [103, 'Krunal', 27]

print("After Appending", dfObj, sep='\n')

Output

  ID Name  Age
a  NaN  NaN  NaN
b  NaN  NaN  NaN
c  NaN  NaN  NaN
After Appending
    ID      Name Age
a  101  Katheryn  29
b  102    Millie  15
c  103    Krunal  27

Conclusion

In this tutorial, we have seen various ways to create a DataFrame without any Data and then append. the Data using various techniques. That is it for creating an empty DataFrame example.

See also

How to check Pandas empty DataFrame

Pandas DataFrame copy()

Pandas DataFrame where()

Pandas mean

Pandas Drop column

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.