26 Oct Join Pandas DataFrame
In Python, we can easily join Pandas DataFrames using the join() method. This will join the columns of two different DataFrames.
Before moving further, we’ve prepared a video tutorial to join Pandas DataFrame:
Let us see an example and create two DataFrames and join them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import pandas as pd # Dataset data1 = { 'id': ["S01", "S02", "S03", "S04", "S05"], 'student': ["Amit", "John", "Jacob", "David", "Steve"], 'roll': [101, 102, 103, 104, 105] } data2 = { 'rank': [1, 4, 3, 5, 2], 'marks': [95, 70, 80, 60, 90] } # DataFrame dataFrame1 = pd.DataFrame(data1) print("DataFrame1 =\n",dataFrame1) dataFrame2 = pd.DataFrame(data2) print("\nDataFrame2 =\n",dataFrame2) # Join two DataFrames resDF = dataFrame1.join(dataFrame2) print("\nJoining DataFrames =\n",resDF) |
Output
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 |
DataFrame1 = id student roll 0 S01 Amit 101 1 S02 John 102 2 S03 Jacob 103 3 S04 David 104 4 S05 Steve 105 DataFrame2 = rank marks 0 1 95 1 4 70 2 3 80 3 5 60 4 2 90 Joining DataFrames = id student roll rank marks 0 S01 Amit 101 1 95 1 S02 John 102 4 70 2 S03 Jacob 103 3 80 3 S04 David 104 5 60 4 S05 Steve 105 2 90 |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments