. Advertisement .
..3..
. Advertisement .
..4..
There are multiple methods to create an empty DataFrame in Pandas and Python. This tutorial will introduce the most useful and straightforward ways.
What Is A DataFrame?
Dataframe is known as a container class used to manipulate and store two-dimensional data. This amount of data often comes in the tabular format in the Pandas data analysis library.
Here, data, columns, and rows are its main components, allowing coders to store various element types. You can store integers in one single column while having string literals in another one.
The ‘dtype’ attribute name can be exploited to return the column’s object type. Sometimes, creating an empty dataframe can save a lot of space and memory.
If you don’t have to append records to the whole dataframe, constructing a new dataframe and appending values to the expected columns will be a better choice.
How To Create An Empty DataFrame
Method 1: Create Without Columns And Rows
Import Python Pandas from the library to build an empty dataframe. Here, you can use the pd.DataFrame()
function if you don’t want to use columns and rows.
The Pandas library’s DataFrame()
class and the class constructors are the same.
Input:
# import pandas library as pd
import pandas as pd
# create an Empty DataFrame object
df = pd.DataFrame()
print(df)
# append columns to an empty DataFrame
df['Name'] = ['Anna', 'Pete', 'Tommy']
df['Scores'] = [97, 600, 200]
df['Questions'] = [2200, 75, 100]
df
Output:
Name Scores Questions
0 Anna 97 2200
1 Pete 600 75
2 Tommy 200 100
Method 2: Create With Columns Only
Starting with some columns is also a great approach. Complete the construction process and add values to it. Then, you can append rows later on. The in-built append()
function can be used as follows:
Input:
# import pandas library as pd
import pandas as pd
# create an Empty DataFrame
# object With column names only
df = pd.DataFrame(columns = ['Name', 'Scores', 'Questions'])
print(df)
# append rows to an empty DataFrame
df = df.append({'Name' : 'Anna', 'Scores' : 97, 'Questions' : 2200},
ignore_index = True)
df = df.append({'Name' : 'Linda', 'Scores' : 30, 'Questions' : 50},
ignore_index = True)
df = df.append({'Name' : 'Tommy', 'Scores' : 17, 'Questions' : 220},
ignore_index = True)
df
Output:
Empty DataFrame
Columns: [Name, Scoress, Questions]
Index: []
Name Scores Questions
0 Anna 97 2200
1 Linda 30 50
2 Tommy 17 220
Method 3: Create With Indices And Column Name
You can also use indices and columns. When building a new dataframe, you will likely pass the indices. At the same time, use the loc()
option to append new rows.
This method retrieves values from a fitted dataframe in columns and rows depending on the passed index value.
Input:
# import pandas library as pd
import pandas as pd
# create an Empty DataFrame object With
# column names and indices
df = pd.DataFrame(columns = ['Name', 'Scores', 'Questions'],
index = ['a', 'b', 'c'])
print("Empty DataFrame With NaN values : \n\n", df)
# adding rows to an empty
# dataframe at existing index
df.loc['a'] = ['Anna', 50, 100]
df.loc['b'] = ['Pete', 60, 120]
df.loc['c'] = ['Tommy', 30, 60]
df
Output:
Empty DataFrame With NaN values :
Name Scores Questions
a NaN NaN NaN
b NaN NaN NaN
c NaN NaN NaN
Name Scores Questions
a Anna 50 100
b Pete 60 120
c Tommy 30 60
Conclusion
It is true that learning to create an empty dataframe is a piece of pie if you follow the right steps. This article has introduced the most highly recommended and simple methods to do this task.
There are also some tips to store data with columns and rows.
Leave a comment