How to create an empty DataFrame and append rows & columns to it in Pandas?
Let’s discuss how to create an empty DataFrame and append rows & columns to it in Pandas n Python. There are multiple ways in which we can do this task. Here we will cover the following section:
- Creating an empty Dataframe in Pandas
- Append row to Dataframe in Pandas
- Append row to Dataframe in Pandas
Creating empty Dataframe
Python3
# import pandas library as pdimport pandas as pd # create an Empty DataFrame objectdf = pd.DataFrame() print(df) |
Output:
Empty DataFrame Columns: [] Index: []
Append Column to Dataframe
Example 1:
Create a complete empty DataFrame without any column name or indices and then append columns in Pandas one by one to it.
Python3
# import pandas library as pdimport pandas as pd # create an Empty DataFrame objectdf = pd.DataFrame() print(df) # append columns to an empty DataFramedf['Name'] = ['Ankit', 'Ankita', 'Yashvardhan'] df['Articles'] = [97, 600, 200] df['Improved'] = [2200, 75, 100] df |
Output:

Example 2:
This method will create a new Dataframe with a new column added to the old Dataframe using assign in Pandas.
Python3
# Import pandas packageimport pandas as pd # Define a dictionary containing Students datadata = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'], 'Height': [5.1, 6.2, 5.1, 5.2], 'Qualification': ['Msc', 'MA', 'Msc', 'Msc']} # Convert the dictionary into DataFramedf = pd.DataFrame(data) # Using 'Address' as the column name and equating it to the listdf2 = df.assign(address=['Delhi', 'Bangalore', 'Chennai', 'Patna']) # Observe the resultprint(df2) |
Output:

Append row to Dataframe
Example 1:
Create an empty DataFrame with columns name only then append rows one by one to it using append() method.
Python3
# import pandas library as pdimport pandas as pd # create an Empty DataFrame# object With column names onlydf = pd.DataFrame(columns = ['Name', 'Articles', 'Improved'])print(df) # append rows to an empty DataFramedf = df.append({'Name' : 'Ankit', 'Articles' : 97, 'Improved' : 2200}, ignore_index = True) df = df.append({'Name' : 'Aishwary', 'Articles' : 30, 'Improved' : 50}, ignore_index = True) df = df.append({'Name' : 'yash', 'Articles' : 17, 'Improved' : 220}, ignore_index = True) df |
Output:

Example 2:
Create an empty DataFrame with a column name and indices and then append rows one by one to it using the loc[] method.
Python3
# import pandas library as pdimport pandas as pd # create an Empty DataFrame object With# column names and indicesdf = pd.DataFrame(columns = ['Name', 'Articles', 'Improved'], index = ['a', 'b', 'c']) print("Empty DataFrame With NaN values : \n\n", df) # adding rows to an empty# dataframe at existing indexdf.loc['a'] = ['Ankita', 50, 100]df.loc['b'] = ['Ankit', 60, 120]df.loc['c'] = ['Harsh', 30, 60] df |
Output:






Please Login to comment...