Pandas Create empty Dataframe and append

Pandas

In this post, we are going to explore multiple ways in Pandas create empty Dataframe and append row by row and rows and columns. Sometimes instead of creating a dataframe from another data collection, we need to create an empty dataframe and append a row, columns later.

What is dataframe() constructor


The dataFrame is a tabular and 2-dimensional labeled data structure frame with columns of data types. It looks like an excel spreadsheet or SQL table, or a dictionary of Series objects. It is the most commonly used pandas object.

DataFrame class constructor is used to create a dataframe.

pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=None)

Parameters

  • Data : It can be an array, iterable(tuple, dictionary, list) or a dataframe.It is optional parmeter.
  • index : It is optional parameter and can be an array.
  • columns :optional,Use to pass the column name.
  • datatype :optional,datatype use to force a dataype for column.
  • copy : optional,It is use to copy data from input

Steps to create a empty dataframe


  • Import the Pandas module using this code import pandas as pd.
  • Use DataFrame() constructor to create a empty dataframe agruments in dataframe construction change as per need.
  • Append the data in form of columns and rows.

Let us start with multiple ways to create a dataframe with the help of examples.

1. Create Empty DataFrame append Data row by row


In the below examples, we will append data in dataframe row by row Using Pandas.Concat() and Append() methods.

1.1Append data row by Row Using Pandas.concat() Method

In this example, we are using the Pandas.concat() Method to concat a row to panda dataframe

import pandas as pd
import numpy as np

 

df_studs = pd.DataFrame()

df_row = pd.DataFrame(data=np.array([['Dev','comp',100]]), columns=['Name','Subj','Marks'])
df_studs = pd.concat([df_studs,df_row], ignore_index=True)

 
print(f'\n{df_studs}')

Output

  Name  Subj Marks
0  Dev  comp   100

1.2.Append data row by row Using Append()

In this below example we are using the append() method to append row by row data to pandas dataframe

import pandas as pd
 

df_studs = pd.DataFrame()

df_row = {'Name':'Dev','Subj':'comp','Marks':100}

df_studs = df_studs.append(df_row, ignore_index=True)

 
print(f'\n{df_studs}')

Output

   Marks Name  Subj
0  100.0  Dev  comp

2. Create empty dataframe without row and columns and append


Here we are creating an empty dataframe with no rows and columns by using the dataframe constructor with no arguments.

Later, We are appending rows and columns data in the empty dataframe.

import pandas as pd
 
#creating an empty dataframe with no rows and columns

df_studs = pd.DataFrame()

print(df_studs)

# appending data using columns name
df_studs['Name'] = ['Jack', 'Max','Tom']
df_studs['Subj'] = ['Math','Phy','Chem']
df_studs['Marks'] = [100,100,100]

print(f'\n{df_studs}')

Output

Empty DataFrame
Columns: []
Index: []

   Name  Subj  Marks
0  Jack  Math    100
1   Max   Phy    100
2   Tom  Chem    100

3. Create empty Dataframe with column and indices and append data


In this example, we have created a dataframe by passing columns names and indices as arguments to the dataframe constructor.

import pandas as pd
 
#creating empty DataFrame with  columns and rows
 
df_studs = pd.DataFrame(columns = ['Name','Subj','marks'] ,index=['Row1','Row2','Row3'])
 
print(df_studs)

Output is an empty dataframe with columns name and indices.

   Name Subj marks
Row1  NaN  NaN   NaN
Row2  NaN  NaN   NaN
Row3  NaN  NaN   NaN

In the below example, we are appending data to the empty dataframe with columns name and indices by using dataframe loc property that we created above.

import pandas as pd
 

df_studs = pd.DataFrame(columns = ['Name','Subj','marks'] ,index=['Row1','Row2','Row3'])


# appending data using indexes
df_studs.loc['Row1'] = ['Jack','Math',100]
df_studs.loc['Row2'] = ['Max','Phy',99]
df_studs.loc['Row3'] = ['Tom','Chem',100,]
 
print(f'\n{df_studs}')

Output

      Name  Subj marks
Row1  Jack  Math   100
Row2   Max   Phy    99
Row3   Tom  Chem   100

4.Create empty dataframe with columns only and append data


We are creating an empty dataframe with columnname only & no rows, by using the dataframe constructor.

We are appending row to the empty dataframe with columnname using the append() method of dataframe.

import pandas as pd
 
#creating an empty dataframe
df_studs = pd.DataFrame(columns = ['Name','Subj','marks'])

print(df_studs)

# appending data using append() method
df_studs=df_studs.append({'Name':'Jack','Subj':'Math','marks':100},ignore_index=True)
df_studs=df_studs.append({'Name':'Max','Subj':'Phy','marks':100},ignore_index=True) 
df_studs=df_studs.append({'Name':'Tom','Subj':'Chem','marks':100},ignore_index=True)
 
print(f'\n{df_studs}')


Output

Empty DataFrame
Columns: [Name, Subj, marks]
Index: []

   Name  Subj marks
0  Jack  Math   100
1   Max   Phy   100
2   Tom  Chem   100

5. Create a empty dataframe with indices and append data


Here in this example, We are creating an empty dataframe with indices only no column name by using a dataframe constructor.

Finally appending column name and rows with data as shown below.

import pandas as pd
 

df_studs = pd.DataFrame(index=['Row1','Row2','Row3'])

print(df_studs)


# appending data using columns name
df_studs['Name'] = ['Jack', 'Max','Tom']
df_studs['Subj'] = ['Math','Phy','Chem']
df_studs['Marks'] = [100,100,100]

print(df_studs)

Output

Empty DataFrame
Columns: []
Index: [Row1, Row2, Row3]

      Name  Subj  Marks
Row1  Jack  Math    100
Row2   Max   Phy    100
Row3   Tom  Chem    100


Summary

In this post, we have learned multiple ways in Pandas Create empty Dataframe and append with examples.