Pandas | Create empty DataFrame in Python

In this post, we will see how to create empty dataframes in Python using Pandas library.
[toc]
Suppose you want to just create empty dataframe, and put data into it later. Let’s see how to create empty dataframe in different ways.

DataFrame is tabular data structure similar to spreadsheets. It contains ordered collections of columns , and each column has data type associated with it.

DataFrame class provides a constructor to create a dataframe using multiple options.

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

Here,
data: It can be any ndarray, iterable or another dataframe.
index: It can be an array, if you don’t pass any index, then index will range from 0 to number of rows -1
columns: Columns are used to define name of any column
dtype: dtype is used to force data type of any column. If you don’t specify dtype, dtype is calculated from data itself.

Create empty dataframe

If you just want to create empty dataframe, you can simply use pd.DataFame().

Here is an example:

# import pandas library
import pandas as pd

#create empty DataFrame

first_df=pd.DataFrame()

print(first_df)

Output:

Empty DataFrame
Columns: []
Index: []

Append data to empty dataframe

You can append data to empty dataframe as below:

# import pandas library
import pandas as pd

#create empty DataFrame

emp_df=pd.DataFrame()

emp_df['Name']=['Mohan','Aryan','Neha']
emp_df['age']=[23,41,24]
emp_df['Gender']=['Male','Male','Female']

print(emp_df)

Output:

    Name  age  Gender
0  Mohan   23    Male
1  Aryan   41    Male
2   Neha   24  Female

Create empty dataframe with columns

If you also know columns of dataframe but do not have any data, you can create dataframe with column names and no data.
Let’s see how to do it.

# import pandas library
import pandas as pd

#create empty DataFrame

first_df=pd.DataFrame(columns = ['Name','Age','Gender'] )

print(first_df)

Output:

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

Append data to empty dataframe with columns

You can append data to empty dataframe with columns as below:

# import pandas library
import pandas as pd

#create empty DataFrame

emp_df=pd.DataFrame(columns = ['Name','Age','Gender'] )

emp_df=emp_df.append({'Name':'Mohan','Age':23,'Gender':'Male'},ignore_index=True)
emp_df=emp_df.append({'Name':'Aryan','Age':41,'Gender':'Male'},ignore_index=True)
emp_df=emp_df.append({'Name':'Neha','Age':24,'Gender':'Female'},ignore_index=True)

print(emp_df)

Output:

    Name  age  Gender
0  Mohan   23    Male
1  Aryan   41    Male
2   Neha   24  Female

Create empty dataframe with columns and indices

You can extend preceding example by adding indices to the rows as below:

# import pandas library
import pandas as pd

#create empty DataFrame

first_df=pd.DataFrame(columns = ['Name','Age','Gender'] ,index=['One','Two','Three'])

print(first_df)

Output:

      Name  Age Gender
One    NaN  NaN    NaN
Two    NaN  NaN    NaN
Three  NaN  NaN    NaN

Append data to empty dataframe with columns and indices

You can add rows with empty dataframe with existing index as below:

# import pandas library
import pandas as pd

#create empty DataFrame

emp_df=pd.DataFrame(columns = ['Name','Age','Gender'] ,index=['One','Two','Three'])

# Add rows using indices
emp_df.loc['One'] = ['Mohan',23,'Male']
emp_df.loc['Two'] = ['Aryan',41,'Male']
emp_df.loc['Three'] = ['Neha',24,'Female']

# Print DataFrame
print(emp_df)

Output:

    Name  age  Gender
0  Mohan   23    Male
1  Aryan   41    Male
2   Neha   24  Female

Create empty dataframe with indices

You can extend preceding example by adding indices to the rows as below:

# import pandas library
import pandas as pd

#create empty DataFrame

first_df=pd.DataFrame(index=['One','Two','Three'])

print(first_df)

Output:

Empty DataFrame
Columns: []
Index: [One, Two, Three]

Append data to empty dataframe with indices

You can add rows with empty dataframe with existing index as below:

# import pandas library
import pandas as pd

#create empty DataFrame
emp_df=pd.DataFrame(index=['One','Two','Three'])

# Add rows using indices
emp_df['Name']=['Mohan','Aryan','Neha']
emp_df['age']=[23,41,24]
emp_df['Gender']=['Male','Male','Female']

# Print DataFrame
print(emp_df)

Output:

        Name  age  Gender
One    Mohan   23    Male
Two    Aryan   41    Male
Three   Neha   24  Female

That’s all about how to create empty dataframes in Python using Pandas.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.