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, and columns later.
What is a 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.
Syntax
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 an optional parameter.
- index : It is an optional parameter and can be an array.
- columns : optional, Use to pass the column name.
- datatype : optional, datatype use to force a datatype for the column.
- copy : optional, It is used to copy data from the input
Steps to create an empty dataframe
Let us start with multiple ways to create a dataframe with the help of examples.
- 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.
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 in Pandas dataFrame 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 in Pandas DataFrame 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 an empty dataframe without rows 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 of 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 column 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 an 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 an 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 names 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 to Create an empty Dataframe and append with examples.