1. Merging data sets
①, many-to-one merger
We need to use the merge function in pandas, where the merge function merges the intersection of two datasets by default (inner connection), and of course other parameters:
How there are inner, outer, left and right, four parameters can be selected, respectively: the intersection, the Union, participate in the merging of the Dataframe, and the
when the column name object is the same: Df1=PD. DataFrame ({'Key':['a','C','a','b','a','C','b','C'],'data1': Range (8)}) DF2=PD. DataFrame ({'Key':['a','b','D'],'data2': Range (3)}) Pd.merge (Df1,df2,on='Key') return key data1 data20A0 01A2 02A4 03B3 14B6 1when the column name object is not the same: Df1=PD. DataFrame ({'Lkey':['a','C','a','b','a','C','b','C'],'data1': Range (8)}) DF2=PD. DataFrame ({'Rkey':['a','b','D'],'data2': Range (3)}) Pd.merge (df1,df2,left_on='Lkey', right_on='Rkey', how=' outer ') return to Lkey data1 Rkey data20A0.0A0.01A2.0A0.02A4.0A0.03C1.0nan nan4C5.0nan nan5C7.0nan nan6B3.0B1.07B6.0B1.08Nan Nan D2.0
②, many-to-many merges
DF1=PD. DataFrame ({'Key':['b','C','b','a','b','a'],'data1': Range (6)}) DF2=PD. DataFrame ({'Key':['a','a','C','b','D'],'data2': Range (5)}) Pd.merge (Df1,df2,on='Key', how=' Right') back to key data1 data20B0.0 31B2.0 32B4.0 33C1.0 24A3.0 05A5.0 06A3.0 17A5.0 18D NaN4
Many-to-many merges produce a Cartesian product of rows, that is, DF1 has 2 a,df2 with 2 A, and rallies produce 4 a
When you need to merge from multiple keys, simply pass in a list of column names.
When merging operations, you need to handle duplicate column names, and the suffixes function can specify strings appended to the duplicate column names of the left and right two Dataframe objects
2. merging on the index
Data Analysis---Data normalization using python