Pandas dataframes are used to process tabular data in python. Sometimes we need to create an empty dataframe to fill in values later. This article discusses how to create empty pandas dataframe in python.
Create an Empty Pandas DataFrame Without Rows or Columns
To create a dataframe without rows or columns, we can use the DataFrame() function defined in the pandas module without any input arguments. After execution, the DataFrame() function returns an empty dataframe. You can observe this in the following example.
import pandas as pd
df=pd.DataFrame()
print("The empty dataframe is:")
print(df)
Output:
The empty dataframe is:
Empty DataFrame
Columns: []
Index: []
In the above output, you can observe that the dataframe contains no columns or indices.
Pandas Empty DataFrame With Column Names
Instead of creating a completely empty dataframe, you can create a dataframe with column names. For this, we will pass the column names to the columns
parameter in the DataFrame() function. The DataFrame() function takes a list of column names as an input argument to the columns
parameter. After execution, it returns an empty dataframe with only column names. You can observe this in the following example.
import pandas as pd
column_names=["A","B","C","D","E"]
df=pd.DataFrame(columns=column_names)
print("The empty dataframe is:")
print(df)
Output:
The empty dataframe is:
Empty DataFrame
Columns: [A, B, C, D, E]
Index: []
In this example, we have created a dataframe with columns A, B, C, D, and E. For this, we passed a list containing the column names to the columns
parameter in the DataFrame() function.
Empty DataFrame With Column Names and Row Indices
With column names, you can also add indices to the rows of an empty dataframe. To create a dataframe with column names and indices, we will pass the list containing column names to the columns
parameter and the list containing indices
to the index parameter of the DataFrame() function. After execution of the DataFrame() function, we will get an empty dataframe with specified column names and indices as shown below.
import pandas as pd
column_names=["A","B","C","D","E"]
indices=[1,2,3,4,5,6,7]
df=pd.DataFrame(columns=column_names,index=indices)
print("The empty dataframe is:")
print(df)
Output:
The empty dataframe is:
A B C D E
1 NaN NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN
3 NaN NaN NaN NaN NaN
4 NaN NaN NaN NaN NaN
5 NaN NaN NaN NaN NaN
6 NaN NaN NaN NaN NaN
7 NaN NaN NaN NaN NaN
In the above example, you can observe that we have created an empty dataframe with given column names and row indices. Here, the empty values in the dataframe are shown as NaN values.
Conclusion
In this article, we discussed how to create empty pandas dataframe in python. We also discussed how to create a dataframe with column names and indices. To learn more about dataframes, you can read this article on how to replace nan with 0 in pandas. You might also like this article on how to compare dataframes in python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.