Wednesday, September 5, 2018

How to Merge Two DataFrame in Pandas | Pandas Tutorial 5 | Big Data Tutorial | Codeing School

This is Pandas Tutorial Part 5. pandas is an open source, the BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.

How to Merge Two DataFrame in Pandas | Pandas Tutorial 5 | Big Data Tutorial | Codeing School
How to Merge Two DataFrame in Pandas, Pandas Tutorial 5


Today’s topic: Merge Two DataFrame.

**1st import pandas module.

import pandas as pd

Take two DataFrame,

df1 = pd.DataFrame([[1,2,3,4],[5,6,7,8],[9,10,11,12]], columns=list('ABCD'))

df1

Output:
A
B
C
D
0
1
2
3
4
1
5
6
7
8
2
9
10
11
12

df2 = pd.DataFrame([[21,32,3,12],[23,45,7,23],[76,8,11,4]], columns=('A1','B1','C1','D1'))

df2

Output:
A1
B1
C1
D1
0
21
32
3
12
1
23
45
7
23
2
76
8
11
4

How to Merge Two DataFrame in Pandas | Pandas Tutorial 5 | Big Data Tutorial | Codeing School
Take-Two DataFrame

Merge Tow DataFrame:

df3 = pd.merge(df1, df2, right_on='C1', left_on='C')

df3

Output:
A
B
C
D
A1
B1
C1
D1
0
1
2
3
4
21
32
3
12
1
5
6
7
8
23
45
7
23
2
9
10
11
12
76
8
11
4

Logic: With the help of this pd.merge(df1, df2, right_on='C1', left_on='C') syntax we can merge two rows. Here, merging is possible because the values of C Column and the values of the C1 column is the same. When two columns value is same, then merging is possible.
Here, left_on: label or list, or array-like. Column or index level names to join on in the left DataFrame. Can also be an array or list of arrays of the length of the left DataFrame. These arrays are treated as if they are columns.
And, right_on: label or list, or array-like. Column or index level names to join on in the right DataFrame. Can also be an array or list of arrays of the length of the right DataFrame. These arrays are treated as if they are columns.
 
How to Merge Two DataFrame in Pandas | Pandas Tutorial 5 | Big Data Tutorial | Codeing School
Merge Two DataFrame

Explore More Read pd.merge()'s Official Documentation. Click Here.




If you have, any quarry please comment below. 

0 comments:

Post a Comment