HOWTO · Pandas

將兩個 Pandas 系列合併到一個 DataFrame 中

本指南演示瞭如何使用 pandas.concat()、pandas.merge()、DataFrame.join() 方法將兩個 Pandas 系列組合或合併到一個 DataFrame 中。

本頁內容

Pandas 是一個非常流行的開源 Python 庫,它提供了各種功能或方法來合併或組合 DataFrame 中的兩個 Pandas 系列。在 pandas 中,series 是一個單一的一維標籤陣列,可以處理整數、浮點數、字串、python 物件等任何資料型別。簡單來說,pandas 系列就是 excel 表格中的一列。系列以順序順​​序儲存資料。

本教程將教我們如何將兩個或多個 Pandas 系列合併或組合成一個 DataFrame

有幾種方法可以將兩個或多個 Pandas 系列合併到一個 DataFrame,例如 pandas.concat()Series.append()pandas.merge()DataFrame.join()。我們將藉助本文中的一些示例簡要詳細地解釋每種方法。

使用 pandas.concat() 方法將兩個 Pandas 系列合併為一個 DataFrame

pandas.concat() 方法沿軸(row-wisecolumn-wise)執行所有連線操作。我們可以沿特定軸合併兩個或多個 Pandas 物件或系列以建立 DataFrameconcat() 方法採用各種引數。

在下面的示例中,我們將 pandas series 傳遞給 merge 和 axis=1 作為引數。axis=1 表示該系列將合併為一列而不是行。如果我們使用 axis=0,它會將 pandas 系列附加為一行。

示例程式碼:

import pandas as pd

# Create Series by assigning names
products = pd.Series(
    ["Intel Dell Laptops", "HP Laptops", "Lenavo Laptops", "Acer Laptops"],
    name="Products",
)
dollar_price = pd.Series([350, 300, 400, 250], name="Price")
percentage_sale = pd.Series([83, 99, 84, 76], name="Sale")

# merge two pandas series using the pandas.concat() method
df = pd.concat([products, dollar_price, percentage_sale], axis=1)
print(df)

輸出:

             Products  Price  Sale
0  Intel Dell Laptops    350    83
1          HP Laptops    300    99
2      Lenavo Laptops    400    84
3        Acer Laptops    250    76

使用 pandas.merge() 方法將兩個 Pandas 系列合併到一個 DataFrame

pandas.merge() 用於合併 DataFrame 的複雜列組合,類似於 SQL joinmerge 操作。merge() 方法可以執行命名系列物件或 DataFrame 之間的所有資料庫連線操作。使用 pandas 時,我們必須向系列傳遞一個額外的引數 name。合併() 方法。

請參閱以下示例。

示例程式碼:

import pandas as pd

# Create Series by assigning names
products = pd.Series(
    ["Intel Dell Laptops", "HP Laptops", "Lenavo Laptops", "Acer Laptops"],
    name="Products",
)
dollar_price = pd.Series([350, 300, 400, 250], name="Price")

# using pandas series merge()
df = pd.merge(products, dollar_price, right_index=True, left_index=True)
print(df)

輸出:

             Products  Price
0  Intel Dell Laptops    350
1          HP Laptops    300
2      Lenavo Laptops    400
3        Acer Laptops    250

使用 Series.append() 方法將兩個 Pandas 系列合併到一個 DataFrame

Series.append() 方法是 concat() 方法的快捷方式。此方法沿 axis=0 或行附加系列。使用這種方法,我們可以通過將系列作為行而不是列附加到另一個系列來建立 DataFrame

我們在原始碼中以如下方式使用了 series.append() 方法:

示例程式碼:

import pandas as pd

# Using Series.append()
technical = pd.Series(["Pandas", "Python", "Scala", "Hadoop"])
non_technical = pd.Series(["SEO", "Graphic design", "Content writing", "Marketing"])

# using the appen() method merge series and create dataframe
df = pd.DataFrame(
    technical.append(non_technical, ignore_index=True), columns=["Skills"]
)
print(df)

輸出:

           Skills
0           Pandas
1           Python
2            Scala
3           Hadoop
4              SEO
5   Graphic design
6  Content writing
7        Marketing

使用 DataFrame.join() 方法將兩個 Pandas 系列合併為一個 DataFrame

使用 DataFrame.join() 方法,我們可以連線兩個系列。當我們使用這種方法時,我們必須將一個系列轉換為 DataFrame 物件。然後我們將使用結果與另一個系列組合。

在以下示例中,我們已將第一個系列轉換為 DataFrame 物件。然後,我們使用這個 DataFrame 與另一個系列合併。

示例程式碼:

import pandas as pd

# Create Series by assigning names
products = pd.Series(
    ["Intel Dell Laptops", "HP Laptops", "Lenavo Laptops", "Acer Laptops"],
    name="Products",
)
dollar_price = pd.Series([350, 300, 400, 250], name="Price")

# Merge series using DataFrame.join() method
df = pd.DataFrame(products).join(dollar_price)
print(df)

輸出:

             Products  Price
0  Intel Dell Laptops    350
1          HP Laptops    300
2      Lenavo Laptops    400
3        Acer Laptops    250

まとめ

我們在本教程中學習瞭如何使用四種不同的方式將兩個 Pandas 系列合併為一個 DataFrame。此外,我們探索了這四種方法 pandas.concat()Series.append()pandas.merge()DataFrame.join() 如何幫助我們解決 Pandas 合併系列任務。