In this post, we will see how to create empty dataframes in Python using Pandas library.
Table of Contents
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.
1 2 3 |
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:
1 2 3 4 5 6 7 8 9 10 |
# import pandas library import pandas as pd #create empty DataFrame first_df=pd.DataFrame() print(first_df) |
Output:
Columns: [] Index: []
Append data to empty dataframe
You can append data to empty dataframe as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# 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:
1 2 3 4 5 6 |
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.
1 2 3 4 5 6 7 8 9 10 |
# import pandas library import pandas as pd #create empty DataFrame first_df=pd.DataFrame(columns = ['Name','Age','Gender'] ) print(first_df) |
Output:
Columns: [Name, Age, Gender] Index: []
Append data to empty dataframe with columns
You can append data to empty dataframe with columns as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# 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:
1 2 3 4 5 6 |
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:
1 2 3 4 5 6 7 8 9 10 |
# 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:
1 2 3 4 5 6 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# 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:
1 2 3 4 5 6 |
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:
1 2 3 4 5 6 7 8 9 10 |
# import pandas library import pandas as pd #create empty DataFrame first_df=pd.DataFrame(index=['One','Two','Three']) print(first_df) |
Output:
1 2 3 4 5 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# 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:
1 2 3 4 5 6 |
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.