Learn how to append DataFrame in Pandas
So as to append a DataFrame, Pandas has DataFrame.append technique.
Nevertheless, the append technique is deprecated since 1.4.0 model.
As an alternative of the append technique, it is best to use Pandas.concat technique so as to add/append information within the information frames.
On this tutorial, we’ll present you examples of utilizing the concat() technique for becoming a member of two or extra information frames in Python applications.
Syntax of concat()
Following is the entire syntax of concat() technique in Pandas:
pandas.concat(objs, *, axis=0, be part of=’outer’, ignore_index=False, keys=None, ranges=None, names=None, verify_integrity=False, type=False, copy=True)
Allow us to use concat() technique with just a few of its parameters beneath.
An instance of concatenating two information frames
On this instance, we have now created an information body based mostly on the Python listing.
This information body is displayed on the display screen.
That is adopted by creating one other Knowledge Body based mostly on listing 2. This information body can be displayed on the display screen.
Then we appended that to the primary DF by concat() technique and displayed concatenated DFs.
Python program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
import pandas as pd
#First Knowledge Body’s information
product_list1 = [ [‘p001’, “Wheat”],
[‘p002’, “Rice”],
[‘p003’, “Sugar”]
]
#Second Knowledge Body’s information
product_list2 = [ [“25.99”,“Out of Stock”],
[“8”,“In Stock”],
[“10”,“In Stock”]
]
#Creating First information body
df_1 = pd.DataFrame (product_list1, columns = [‘Product ID’, ‘Product Name’])
print(“**********Knowledge Body 1:*********”)
print(df_1)
#Second Knowledge body based mostly on product_list2
df_2 = pd.DataFrame (product_list2, columns = [‘Price’, ‘Status’])
print(“**********Knowledge Body 2:*********”)
print(df_2)
#Concatenating each Knowledge Frames
print(“**********Knowledge Body 1 and a couple of:***********”)
print(pd.concat([df_1, df_2], axis=1)) |
Output:
An instance of utilizing concat() technique with Dict
Within the above instance, you may have seen the columns are added forward after we concatenated the primary DF to the second DF.
Within the instance beneath, we’ll create two dictionaries in Python.
The primary dictionary incorporates Column names and three data of the phone listing (Title and Cellphone No). On that bases, we created a DataFrame object.
This information body is displayed on the display screen.
That is adopted by creating one other dictionary which is used for making a second Knowledge Body. There we created a row to be appended on the finish of the primary DF.
For readability, information frames earlier than and after concatenation are displayed:
Python program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
import pandas as pd
#Creating first dictionary with columns and three rows
tel_dir1 = {‘Title’: [‘Mike’,‘Haynes’,‘Mina’],
‘Cellphone No’:[123445689,45678910, 635363636]}
#Second dictionary with columns and a row
tel_dir2 = {‘Title’: [‘Raina’],
‘Cellphone No’:[458745141]}
df_1 = pd.DataFrame.from_dict(tel_dir1)
print(“**********Knowledge Body 1:***********”)
print(df_1)
#Create information body based mostly on second dict
df_2 = pd.DataFrame.from_dict(tel_dir2)
#Show information body 2
print(“**********Knowledge Body 2:***********”)
print(df_2)
#Concatenating each Knowledge Frames
print(“**********Knowledge Body 1 and a couple of after appending:***********”)
print(pd.concat([df_1, df_2], axis=0)) |
Consequence:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
**********Knowledge Body 1:***********
Title Cellphone No
0 Mike 123445689
1 Haynes 45678910
2 Mina 635363636
**********Knowledge Body 2:***********
Title Cellphone No
0 Raina 458745141
**********Knowledge Body 1 and 2 after appending:***********
Title Cellphone No
0 Mike 123445689
1 Haynes 45678910
2 Mina 635363636
0 Raina 458745141 |
Discover the axis parameter within the concat technique. This time we used axis=0.
Because of this, the second information body is appended on the finish of the primary DF, and although the second DF additionally contained column names, the concatenated end result doesn’t embrace this.
So, if we have now used axis = 1, as within the first instance the end result:
An instance of concatenating two information frames and utilizing keys parameter
You might also use the keys parameter of the concat() technique for developing the hierarchical index.
In that case, the handed keys are used on the outermost degree.
See the instance beneath the place we created three information frames and concatenated together with keys:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
import pandas as pd
# creating three DFs
df_1 = pd.DataFrame({‘Col1’: [‘X’, ‘Y’, ‘Z’],
‘Col2’: [‘X’, ‘Y’, ‘Z’]})
#Show information body 1
print(“**********Knowledge Body 1:*********”)
print(df_1)
df_2 = pd.DataFrame({‘Col1’: [‘A’, ‘B’, ‘C’],
‘Col2’: [‘A’, ‘B’, ‘C’]})
#Show information body 2
print(“**********Knowledge Body 2:*********”)
print(df_2)
df_3 = pd.DataFrame({‘Col1’: [‘I’, ‘J’, ‘K’],
‘Col2’: [‘I’, ‘J’, ‘K’]})
#Show information body 3
print(“**********Knowledge Body 3:*********”)
print(df_3)
# Concatenating three DFs with keys
print(‘**********After concatenating:**********’)
print(pd.concat([df_1, df_2, df_3],
keys = [‘DF_1’, ‘DF_2’,‘DF_3’])) |
Output: