top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

How to implement Pandas in Python: merge Datasets

Hi, this might sound crazy but it's true. I like Panda Animal, when I heard about the pandas library in python it grabs my attention, and quickly I fell in love with the pandas program. why? that you will come to know, please keep reading till the end.



Let’s go through a few basic questions about Pandas:


What is Pandas in Python?

Pandas is an open-source Python package that provides numerous tools for data analysis. The package comes with several data structures that can be used for many different data manipulation tasks.

Why do we need pandas in python?

Pandas is the most popular Python library that is used for data analysis. It provides highly optimized performance with back-end source code that is purely written in C or Python. It is very easy to write and understand.



To implement pandas in python, install the Pandas library in Python:

 pip install pandas



Now that we have pandas install in Python, we are ready to write our program


Let's write a program to merge two datasets?


To begin with this program, we need to understand

what is the use of merge() function in Pandas?

merge() function brings two data frame(datasets) together, instead of merge we can use join(), Concat() etc. function as well.


what are datasets(DataFrames)?

datasets are nothing but a set of rows and columns


below, I have created one DataFrame df1

Note: please don't forget to import pandas by writing

import pandas as pd 


import pandas as pd
    df1 = pd.DataFrame({
    'Name': ['Jose','Ronny', 'Elen', 'April', 'Nancy'],
    'Date_Of_Birth': ['17/05/2002','16/02/1999','25/09/1998','11/05/2002','15/09/1997'],
    'Age': [18.5, 21.2, 22.5, 22, 23]})
    
    print(df1)  
    
    output:
Name Date_Of_Birth    Ag
0   Jose     17/05/2002  18.5
1  Ronny     16/02/1999  21.2
2   Elen     25/09/1998  22.5
3  April     11/05/2002  22.0
4  Nancy     15/09/1997  23.0

create another DataFrame df2

df2 = pd.DataFrame({
    'Name': ['Arold','Ronny', 'Elen', 'April', 'Nancy'],
    'Date_Of_Birth ': ['23/10/2012','16/02/1999','25/09/1998','11/05/2002','15/09/1997'],
    'Age': [29, 21.2, 22.5, 22, 23]})
    
    print(df2)
    
    output:
    Name Date_Of_Birth    Age
    0  Arold     23/10/2012  29.0
    1  Ronny     16/02/1999  21.2
    2   Elen     25/09/1998  22.5
    3  April     11/05/2002  22.0
    4  Nancy     15/09/1997  23.0
    

merge two DataFrames df1 and df2 by using the merge() function

pd.merge()

df3 = pd.merge(df1,df2)
print("Merge Dataset:")
print(df3)


output:
Merge Dataset:
 Name Date_Of_Birth    Age
 0  Ronny     16/02/1999  21.2
 1   Elen     25/09/1998  22.5
 2  April     11/05/2002  22.0
 3  Nancy     15/09/1997  23.0

Conclusion: Thanks for reading Blog. pandas programs are really simple to write and easy to understand. Please stay connected!










135 views0 comments

Kommentare

Mit 0 von 5 Sternen bewertet.
Noch keine Ratings

Rating hinzufügen
bottom of page