Python | Merging, merging and merging DataFrames with Panda

NumPy | Python Methods and Functions

Below is the implementation using

# Python merge program
# dataframes using Panda

 
# Create first data frame

df1   = pd.DataFrame ({ `A` : [ ` A0` , `A1` , `A2` , `A3` ],

  `B` : [ ` B0` , `B1` , ` B2` , `B3` ],

  `C` : [ ` C0` , `C1` , ` C2` , `C3` ],

`D` : [ ` D0` , `D1` , ` D2` , `D3` ]},

index = [ 0 , 1 , 2 , 3 ])

 
# Create second data frame

df2 = pd.DataFrame ({ ` A` : [ ` A4` , `A5` , `A6` , ` A7` ],

`B` : [ `B4` , ` B5` , `B6` , ` B7` ],

`C` : [ `C4` , `C5` , ` C6` , `C7`  ],

`D` : [ `D4` , ` D5` ​​ , `D6` , `D7` ]},

  index = [ 4 , 5 , 6 , 7 ])

 
# Create third data frame

df3 = pd.DataFrame ({ ` A` : [ `A8` , `A9` , ` A10` , ` A11` ],

` B` : [ `B8` , `B9` , `B10` , ` B11` ],

`C` : [ `C8` , ` C9` , `C10` , ` C11` ], < / p>

`D` : [ `D8` , ` D9` , `D10` , ` D11` ]},

index = [ 8 , 9 , 10 , 11 ])

 
# Data concatenation
pd.concat ([df1, df2, df3])

Exit:

Code # 2: merging data frames
Pandas provides a single merge () function as entry points for all standard database connection operations between DataFrame objects.

# Python merge program
# dataframes using Panda

  
# Dataframe created

left = pd.DataFrame ({ `Key` : [ ` K0` , `K1` , ` K2` , `K3` ],

`A` : [ `A0` , ` A1 ` , ` A2` , `A3` ],

  ` B` : [ `B0` , `B1` , ` B2` , ` B3` ]})

  

right = pd .DataFrame ({ `Key` : [ ` K0` , ` K1` , `K2` , `K3` ],

`C` : [ `C0` , ` C1` , `C2` , ` C3` ] ,

`D` : [ `D0` , ` D1` , `D2` , ` D3` ]})

 
# Data merge х

pd.merge (left, right, how = `inner` , on = ` Key` )

Exit:

Code # 3: merging dataframes

# Python program to attach
# dataframes using Panda

  

left = pd.DataFrame ({ `A` : [ `A0` , `A1` , ` A2` , `A3` ],

  `B` : [ ` B0` , `B1` , ` B2` , `B3` ]},

index = [ `K0` , ` K1` , `K2` , ` K3 ` ])

  

right = pd.DataFrame ({ `C` : [ `C0` , ` C1` , `C2` , ` C3` ],

` D` : [ `D0` , `D1` , ` D2` , ` D3` ]},

index = [ `K0` , ` K1` , `K2` , ` K3` ] )

 
# Attach to data frames
left.join (right)

Exit:





Get Solution for free from DataCamp guru