Append data to an empty Pandas DataFrame


Introduction

A data structure known as a data frame is a two-dimensional labelled array with columns that might be of various data kinds. You can compare it to a spreadsheet, a SQL table, or even a dict of Series objects to better understand it. It is the panda item that is used the vast majority of the time.

In addition to the data itself, you have the option of also passing parameters for the index (row labels) and columns (column labels). If you supply an index and/or columns, you are assuring that those elements will be present in the DataFrame that is produced as a consequence of your call. Therefore, if you combine a dict of Series with a certain index, it will throw away all data that does not match up to the provided index.

In this article we will be learning how to append data to an empty Pandas Dataframe.

Pandas

Pandas is a free and open-source library that was developed largely for the purpose of working with relational or labelled data in a straightforward and intuitive manner. Its principal objective is to make tasks of this kind easier to do. It provides a broad range of data structures and operations, all of which can be put to use in the process of manipulating numerical data and time series. The creation of this library is based on the NumPy library, which acts as the basis. Pandas is very fast, and it offers its users outstanding speed and productivity.

Wes McKinney came up with the idea for Pandas back in 2008, while he was still employed at AQR Capital Management. He was successful in persuading the AQR to grant him permission to open source the Pandas. In 2012, another employee of AQR named Chang She became the second big contributor to the library. Pandas have been released in a variety of iterations throughout the course of time. Pandas are now running version 1.5.2, which was made available for download on November 22nd, 2022.

Dataframe

A data structure known as a data frame is a two-dimensional labelled array with columns that might be of various data kinds. You can compare it to a spreadsheet, a SQL table, or even a dict of Series objects to better understand it. It is the panda item that is used the vast majority of the time. Pandas DataFrame is a tabular data structure that is two-dimensional, its size can be changed, it can include possibly different types of data, and it has named axes (rows and columns). A data frame is a kind of data structure that is two-dimensional, meaning that the data is organized in a tabular form with rows and columns. The data, rows, and columns are the three primary components that make up a Pandas DataFrame.

You have the option of providing parameters for the index (row labels), as well as parameters for the columns, in addition to the data itself (column labels). When you make a call to a method and provide an index and/or columns, you ensure that those components will be included in the DataFrame that is generated as a result of the execution of that method. Therefore, if you combine a dict of Series with a certain index, it will discard any data that does not correspond to the given index and only keep the data that does match.

How to create an empty Dataframe?

By getting pandas from the Python library, you can make an empty dataframe. Later, use pd.DataFrame() to make a dataframe with no rows or columns, like the example below shows. Note that the pandas library's DataFrame() class is similar to the constructor, which is used to build the class.

Code Example

import pandas as pdd
 
dtf = pdd.DataFrame()
#an empty dataframe is created using pandas 
print(dtf)

Output

Empty DataFrame
Columns: []
Index: []

How to append data to an empty Dataframe?

Appending a row to an empty dataframe

import pandas as pdd
dtf_p = pdd.DataFrame()

#an empty data frame is created using pandas 
print(dtf_p)
d3 = pd.DataFrame(
   [["ID","Name","GPA","Course","Country"],
      [128, "Siri",1.8,"BA","US" ],
   [18, "Georgia",2,"MTech", "UK"]],
   
)
dtf_p=dtf_p.append(d3,ignore_index=False)
dtf_p  

Output

Appending a Column to an empty dataframe

import pandas as pdd
 
dtf_p = pdd.DataFrame()
#an empty data frame is created using pandas 
print(dtf_p)
add = ['Delhi', 'Bangalore', 'Chennai', 'Patna']
name=['Shruti','Amiolika','Saba','Parul']
dtf_p['Name'] = name
dtf_p['Address'] = add
print(dtf_p)

Output

Empty DataFrame
Columns: []
Index: []
       Name    Address
0    Shruti      Delhi
1  Amiolika  Bangalore
2      Saba    Chennai
3     Parul      Patna

Conclusion

In this article, we have learned about what is pandas library, what is a data frame, how to implement an empty data frame and how to append rows and columns to a data frame.

Updated on: 24-Aug-2023

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements