26 Oct Concatenate Pandas DataFrames
In this lesson, learn to concatenate Pandas DataFrames in Python using the concat() method. This will concatenate the content of the DataFrames.
Let us see an example and create two DataFrames and concatenate 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 25 |
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 = { 'id': ["S06", "S07", "S08"], 'student': ["Ben", "Kane", "Rohit"], 'roll': [106, 107, 108] } # DataFrame dataFrame1 = pd.DataFrame(data1, index=[0, 1, 2, 3, 4]) print("DataFrame1 =\n",dataFrame1) dataFrame2 = pd.DataFrame(data2, index=[5, 6, 7]) print("\nDataFrame2 =\n",dataFrame2) # Concatenating two DataFrames resDF = pd.concat([dataFrame1, dataFrame2]) print("\nConcatenating 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 26 |
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 = id student roll 5 S06 Ben 106 6 S07 Kane 107 7 S08 Rohit 108 Concatenating DataFrames = id student roll 0 S01 Amit 101 1 S02 John 102 2 S03 Jacob 103 3 S04 David 104 4 S05 Steve 105 5 S06 Ben 106 6 S07 Kane 107 7 S08 Rohit 108 |
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