Python

Pandas DataFrame Append

Pandas is the special, analysis-based package of python especially utilized to evaluate and smuggle python data. It comes up with the “DataFrame()” function that has been popular for aligning the python data in rows and columns, i.e. matrix form. As the name suggests, the “append()” function is utilized to add something on some structure’s end. Therefore, we can say that pandas the append() function must be used to add data from one data frame to the end of another data frame. Thus, we will be implementing some python examples in Spyder 3 to see the working of pandas DataFrame.Append() function in Windows 10. Let’s start.

Example 01:

Let’s start with the first example to see how python data frames can be initialized with pandas. Within the Spyder 3 python tool, we have imported the panda’s package with the “import” keyword as “pd” object. This will be used to initialize the data frame in the code. So, we have added a new variable d1, getting the new pandas data frame via the “pd” object utilizing the “DataFrame()” function. The DataFrame() function is here to create a tabular form data frame while adding two lists of 2*2, i.e. rows into columns. The columns have been named as 1 and 2 using the list structure on them. The indexes for rows have been declared as 1 and 2. The print statement is here to print out the data frame “d1” on the console.

After the first data frame creation, we have also created another data frame with the same method. The only change is here within the value lists, i.e. different values. The print statement displays the second data frame d2 on the console. Now, the second last print statement tells us that we will display the append data frame. So, the second data frame d2 has been appended at the last of the first data frame, d2 using the append() function. The print statement displays the appended data frame.

import pandas as pd
d1 = pd.DataFrame([['a', 'b'], ['d', 'e']], columns=list('12'), index=['1', '2'])
print("Dataframe 1 ...")
print(d1)
d2 = pd.DataFrame([['c', 'f'], ['g', 'h']], columns=list('12'), index=['1', '2'])
print("Dataframe 2 ...")
print(d2)
print("Appended Dataframe...")
print(d1.append(d2))

After the code has been completed, it’s time to execute this python code to see the results. Make use of the Spyder 3 run button from the taskbar and go ahead. In return, we have got the below output. It shows the first and second data frames separately. After that, the second data frame appended to the first data frame has been displayed in the output.

Example 02:

Let’s take a look at another example for using the append() function to join two data frames of pandas. This time, we have been utilizing dictionaries to create data frames. So, we have started the program to import the pandas package as “pd”. Two data frames, d1 and d2, have been created and initialized using pandas’s DataFrame() function with the object “pd”. We have initialized a library in both the data frames having two key-pair values. The key is some alphabet or character and same in both data frames “x” and “y”. While the pair of the keys “x” and “y” are two lists of totally different values in each for both data frames. The first two print statements are here to display the data frames d1 and d2 separately with a line break by “\n”. While the last print statement utilizes the append() function in it to join the second data frame d2 with the first data frame d1 and display it on the shell as one.

import pandas as pd    
d1 = pd.DataFrame({"x":[1, 3, 5], "y":[2, 4, 6]})  #using dictionary
d2 = pd.DataFrame({"x":[7, 9, 11], "y":[8, 10, 12]}) #using dictionary
print(d1, "\n")
print(d2, "\n")
print(d1.append(d2))

After running this code, we have got the data frames displayed separately and then jointly.

Example 03:

Let’s take a look at our last example of using the panda’s data frame with append() function to join them in one. This time, we have been starting our code by creating 2 string type dictionaries, dic1 and dic2, after importing the panda’s package as object “pd”. Both the dictionaries dic1 and dic2 have 3 key-pair values. The keys are of string types, while the first two values are string type lists, and the last key value is integer type lists. The dic1 and dic2 have been converted to a pandas data frame by calling the DataFrame function with the pandas object “pd”. The data frames are saved to d1 and d2. Now, the append() function is used to join d1 with d1 and saved to the variable d3. The d3 joint data frame is printed out with the print() function.

import pandas as pd
dic1 = {
    'Name': ['John', 'William', 'Laila'],
    'FName': ['Jack', 'Worth', 'Sky'],
    'Age': [36, 50, 25]
}
dic2 = {
    'Name':['Elizebath', 'Diana', 'Marshal'],
    'FName':['Patinson', 'Penty', ''],
   'Age': [56, 25, 29]
}
d1 = pd.DataFrame(dic1)
d2 = pd.DataFrame(dic2)
d3 = d1.append(d2)
print("\nThe appended dataframe:\n", d3)

The data frames have been appended and displayed as per the output.

Conclusion:

This article has covered the use of pandas DataFrame() and append() function in python while utilizing the Spyder 3 tool. We have utilized the lists and dictionaries of integer, characters and string types to create data frames and then append them together. We hope this tutorial proves useful while using Spyder 3 or any other Python tool.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content