使用 Pandas 合并、连接和串联 DataFrame

pandasserver side programmingprogramming

在本教程中,我们将学习使用 pandas  库对 DataFrames  进行合并、连接和串联。我想您已经熟悉 DataFrame 和 Pandas 库了。让我们逐一了解这三种操作。

合并

我们有一个名为 pandas.merge()  的方法,它可以像数据库连接操作一样合并 DataFrame。按照以下步骤操作即可获得所需的输出。 Merge 方法使用公共列进行合并操作。

  • 初始化 Dataframes。

  • 调用 pandas.merge() 方法,并传入三个参数:dataframes、how(定义数据库连接操作)、on(Dataframes 的公共字段)。

示例

我们来看一个例子。

# 导入 Pandas 库
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

Join

merge 方法类似,我们有一个方法 dataframe.join(dataframe) 用于连接数据框。让我们看看将两个数据框连接成一个的步骤。join 方法使用数据框的 index

  • 初始化数据框。

  • 编写语句 dataframe_1.join(dataframe_2) 进行连接。

示例

让我们使用代码示例来尝试一下。

# 导入 Pandas 库
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)
# 打印新的数据框
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

连接

合并 方法类似,我们有一个名为 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

结论

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


相关文章