📜  合并两个条件复杂的 Pandas DataFrame

📅  最后修改于: 2022-05-13 01:55:14.319000             🧑  作者: Mango

合并两个条件复杂的 Pandas DataFrame

在本文中,我们将讨论如何合并具有一些复杂条件的两个 Pandas Dataframe。 Pandas 中的数据帧可以使用pandas.merge()方法进行合并。

在处理数据集时,可能需要合并具有某些复杂条件的两个数据框,以下是合并具有某些复杂条件的两个数据框的一些示例。

示例 1:

使用 merge()函数合并两个数据帧,参数为两个数据帧。



Python3
# importing pandas as pd
import pandas as pd
  
# creating dataframes
df1 = pd.DataFrame({'ID': [1, 2, 3, 4], 
                    'Name': ['John', 'Tom', 'Simon', 'Jose']})
  
df2 = pd.DataFrame({'ID': [1, 2, 3, 5], 
                    'Class': ['Second', 'Third', 'Fifth', 'Fourth']})
  
# merging df1 and df2 with merge function
df = pd.merge(df1, df2)
print(df)


Python3
# importing pandas as pd
import pandas as pd
  
# creating dataframes
df1 = pd.DataFrame({'Name': ['John', 'Tom', 'Simon', 'Jose'],
                    'Age': [5, 6, 4, 5]})
  
df2 = pd.DataFrame({'Name': ['John', 'Tom', 'Jose'],
                    'Class': ['Second', 'Third', 'Fifth']})
  
# merging df1 and df2 with merge function 
# with the common column as Name
df = pd.merge(df1, df2, on='Name')
print(df)


Python3
# importing pandas as pd
import pandas as pd
  
# creating dataframes
df1 = pd.DataFrame({'Name': ['John', 'Tom', 'Simon', 'Jose'], 
                    'Age': [5, 6, 4, 5]})
  
df2 = pd.DataFrame({'Name': ['John', 'Tom', 'Jose'],
                    'Class': ['Second', 'Third', 'Fifth']})
  
# merging df1 and df2 with merge function 
# with the common column as Name
df = pd.merge(df1, df2, on='Name', how="left")
print(df)


Python3
# importing pandas as pd
import pandas as pd
  
# creating dataframes
df1 = pd.DataFrame({'Name':['John', 'Tom', 'Simon', 'Jose'],
                    'Age':[5, 6, 4, 5]})
  
df2 = pd.DataFrame({'Name':['John', 'Tom', 'Philip'],
                    'Class':['Second', 'Third', 'Fifth']})
  
# merging df1 and df2 with merge function 
# with the records of both the dataframes
df = pd.merge(df1, df2, how = "outer")
print(df)


Python3
# importing pandas as pd
import pandas as pd
  
# creating dataframes
df1 = pd.DataFrame({'Name':['John', 'Tom', 'Simon', 'Jose'],
                    'Age':[5, 6, 4, 5]})
  
df2 = pd.DataFrame({'Name':['John', 'Tom', 'Simon', 'Tom'],
                    'Class':['Second', 'Third', 'Fifth', 'Fourth']})
  
# merging df1 and df2 with merge function 
# with the records of both the dataframes
df = pd.merge(df1, df2, how = 'left', indicator = True)
print(df)


Python3
# importing pandas as pd
import pandas as pd
  
# creating dataframes
df1 = pd.DataFrame({'Name':['John', 'Tom', 'Simon', 'Jose'],
                    'Age':[5, 6, 4, 5]})
  
df2 = pd.DataFrame({'Name':['John', 'Tom', 'Simon', 'Tom'],
                    'Class':['Second', 'Third', 'Fifth', 'Fourth']})
  
# merging df1 and df2 with merge function with 
# the one to many relations from both the dataframes
df = pd.merge(df1, df2, validate = 'one_to_many')
print(df)


输出 :

示例 2:

在数据框的某些指定列名上使用 merge()函数合并两个数据框。

蟒蛇3

# importing pandas as pd
import pandas as pd
  
# creating dataframes
df1 = pd.DataFrame({'Name': ['John', 'Tom', 'Simon', 'Jose'],
                    'Age': [5, 6, 4, 5]})
  
df2 = pd.DataFrame({'Name': ['John', 'Tom', 'Jose'],
                    'Class': ['Second', 'Third', 'Fifth']})
  
# merging df1 and df2 with merge function 
# with the common column as Name
df = pd.merge(df1, df2, on='Name')
print(df)

输出 :



示例 3:

将两个数据帧与第一个数据帧中的所有值合并,将第二个数据帧中不匹配的值合并为 NaN。同样可以合并第二个数据框的所有值,我们要做的只是在合并时给出数据框的位置为左或右。

蟒蛇3

# importing pandas as pd
import pandas as pd
  
# creating dataframes
df1 = pd.DataFrame({'Name': ['John', 'Tom', 'Simon', 'Jose'], 
                    'Age': [5, 6, 4, 5]})
  
df2 = pd.DataFrame({'Name': ['John', 'Tom', 'Jose'],
                    'Class': ['Second', 'Third', 'Fifth']})
  
# merging df1 and df2 with merge function 
# with the common column as Name
df = pd.merge(df1, df2, on='Name', how="left")
print(df)

输出 :

示例 4:

使用带有外部联接的合并函数将两个数据帧与两个数据帧的所有值合并。也可以使用内部连接来连接两个数据框。

蟒蛇3

# importing pandas as pd
import pandas as pd
  
# creating dataframes
df1 = pd.DataFrame({'Name':['John', 'Tom', 'Simon', 'Jose'],
                    'Age':[5, 6, 4, 5]})
  
df2 = pd.DataFrame({'Name':['John', 'Tom', 'Philip'],
                    'Class':['Second', 'Third', 'Fifth']})
  
# merging df1 and df2 with merge function 
# with the records of both the dataframes
df = pd.merge(df1, df2, how = "outer")
print(df)

输出 :



示例 5:

将数据帧与指标值合并以查看哪个数据帧具有该特定记录。

蟒蛇3

# importing pandas as pd
import pandas as pd
  
# creating dataframes
df1 = pd.DataFrame({'Name':['John', 'Tom', 'Simon', 'Jose'],
                    'Age':[5, 6, 4, 5]})
  
df2 = pd.DataFrame({'Name':['John', 'Tom', 'Simon', 'Tom'],
                    'Class':['Second', 'Third', 'Fifth', 'Fourth']})
  
# merging df1 and df2 with merge function 
# with the records of both the dataframes
df = pd.merge(df1, df2, how = 'left', indicator = True)
print(df)

输出 :

示例 6:

合并两个数据帧中具有一对多关系的数据帧。同样可以合并多对多、一对一和一对多类型的关系。

蟒蛇3

# importing pandas as pd
import pandas as pd
  
# creating dataframes
df1 = pd.DataFrame({'Name':['John', 'Tom', 'Simon', 'Jose'],
                    'Age':[5, 6, 4, 5]})
  
df2 = pd.DataFrame({'Name':['John', 'Tom', 'Simon', 'Tom'],
                    'Class':['Second', 'Third', 'Fifth', 'Fourth']})
  
# merging df1 and df2 with merge function with 
# the one to many relations from both the dataframes
df = pd.merge(df1, df2, validate = 'one_to_many')
print(df)

输出 :