使用Pandas合并,联接和连接DataFrame

在本教程中,我们将学习使用pandas 库合并,联接和连接DataFrame 。我认为您已经熟悉数据框和熊猫库。让我们一一看这三个操作。

合并

我们有一个名为pandas.merge()的方法,该方法类似于数据库联接操作 来合并数据帧。请按照以下步骤获得所需的输出。合并 方法将公共列用于合并操作。

  • 初始化数据框。

  • 调用带有三个参数数据帧的方法pandas.merge(),如何(定义数据库联接操作)(数据帧的公共字段)上。

示例

让我们来看一个例子。

# importing the pandas library
import pandas
# creating dataframes
    dataframe_1 = pandas.DataFrame({"Common": ["A", "B", "C", "D", "E"],
   "Name": ["John", "Alice", "Emma", "Watson", "Harry"], "Age": [18, 19, 20, 21, 15]}) dataframe_2 =     pandas.DataFrame({"Common": ["A", "B", "C", "D", "E"], "Sport": ["Cricket", "Football", "Table     Tennis", "Badminton", "Chess"], "Movie": ["Jumanji", "Black Widow", "End Game", "Mr. Robot",          "Matrix"]})
# merging using merge method
# how = left or right or inner
new_df = pandas.merge(dataframe_1, dataframe_2, how="left", on="Common") # printing the resultant dataframe print(new_df)

输出结果

如果运行上面的代码,您将得到以下结果。

Common Name   Age  Sport            Movie
0   A  John   18  Cricket         Jumanji
1   B  Alice  19  Football    Black Widow
2   C  Emma   20  Table Tennis   End Game
3   D  Watson 21  Badminton     Mr. Robot
4   E  Harry  15  Chess            Matrix

加入

merge方法类似,我们有一个称为dataframe.join(dataframe)的方法用于连接数据框。让我们看看将两个数据框合并为一个的步骤。join方法使用数据帧的索引

  • 初始化数据帧。

  • 编写一个语句dataframe_1.joindataframe_2)加入。

示例

让我们尝试一下编码示例。

# importing the pandas library
import pandas
# creating dataframes
   dataframe_1 = pandas.DataFrame({"Name": ["John", "Alice", "Emma", "Watson", "Harry"], "Age": [18,    19, 20, 21, 15]}, index = ["A", "B", "C", "D", "E"])dataframe_2 = pandas.DataFrame({"Sport":          ["Cricket", "Football", "Table Tennis", "Badminton", "Chess"], "Movie": ["Jumanji", "Black Widow",    "End Game", "Mr. Robot", "Matrix"]}, index = ["A", "B", "C", "D", "E"])
   # joining
   new_df = dataframe_1.join(dataframe_2)
# printing the new dataframe
print(new_df)

如果运行上述程序,将得到以下输出

输出结果

     Name   Age   Sport           Movie
A    John   18   Cricket        Jumanji
B    Alice  19   Football   Black Widow
C    Emma   20   Table Tennis  End Game
D    Watson  21  Badminton    Mr. Robot
E    Harry   15   Chess          Matrix

级联

merge和join方法类似,我们有一个称为pandas.concat(list-> dataframes)的方法来连接数据帧。让我们看看连接数据帧的步骤。串联将数据帧合并为一个。

  • 初始化数据帧。

  • 使用pandas.concat([df_1,df_2,..])连接数据帧。打印结果。

示例

让我们尝试一下编码示例。

# importing the pandas library
import pandas
   # creating dataframes dataframe_1 = pandas.DataFrame({"Name":                                        ["John","Alice","Emma","Watson","Harry"], "Age": [18, 19, 20, 21, 15]}, index = ["A", "B", "C",       "D", "E"]) dataframe_2 = pandas.DataFrame({"Name": ["Wick", "Robert", "Elliot", "Baby",                "Cruise"], "Age": [22, 20, 45, 15, 42]}, index = ["F", "G", "H", "I", "J"])
   # concatenating -> you can pass any number of new_df = pandas.concat([dataframe_1, dataframe_2])
# printing the new dataframe
print(new_df)

输出结果

如果运行上述程序,将得到以下输出。

   Name    Age
A  John     18
B Alice     19
C Emma      20
D Watson    21
E Harry     15
F Wick      22
G Robert    20
H Elliot    45
I Baby      15
J Cruise    42

结论

如果您对本教程有任何疑问,请在评论部分中提及。