Pandas DataFrame 基于其他列创建新列

  1. Pandas DataFrame 中根据其他列的值按元素操作创建新列
  2. 使用 DataFrame.apply() 方法在 Pandas DataFrame 中根据其他列的值创建新列
Pandas DataFrame 基于其他列创建新列

本教程将介绍我们如何在 Pandas DataFrame 中根据 DataFrame 中其他列的值,通过对列的每个元素应用函数或使用 DataFrame.apply() 方法来创建新的列。

import pandas as pd

items_df = pd.DataFrame(
    {
        "Id": [302, 504, 708, 103, 343, 565],
        "Name": ["Watch", "Camera", "Phone", "Shoes", "Laptop", "Bed"],
        "Cost": [300, 400, 350, 100, 1000, 400],
        "Discount(%)": [10, 15, 5, 0, 2, 7],
    }
)

print(items_df)

输出:

    Id    Name  Cost  Discount(%)
0  302   Watch   300           10
1  504  Camera   400           15
2  708   Phone   350            5
3  103   Shoes   100            0
4  343  Laptop  1000            2
5  565     Bed   400            7

我们将使用上面代码片段中显示的 DataFrame 来演示如何根据 DataFrame 中其他列的值在 Pandas DataFrame 中创建新的列。

Pandas DataFrame 中根据其他列的值按元素操作创建新列

import pandas as pd

items_df = pd.DataFrame(
    {
        "Id": [302, 504, 708, 103, 343, 565],
        "Name": ["Watch", "Camera", "Phone", "Shoes", "Laptop", "Bed"],
        "Actual Price": [300, 400, 350, 100, 1000, 400],
        "Discount(%)": [10, 15, 5, 0, 2, 7],
    }
)

print("Initial DataFrame:")
print(items_df, "\n")

items_df["Final Price"] = items_df["Actual Price"] - (
    (items_df["Discount(%)"] / 100) * items_df["Actual Price"]
)


print("DataFrame after addition of new column")
print(items_df, "\n")

输出:

Initial DataFrame:
    Id    Name  Actual Price  Discount(%)
0  302   Watch           300           10
1  504  Camera           400           15
2  708   Phone           350            5
3  103   Shoes           100            0
4  343  Laptop          1000            2
5  565     Bed           400            7 

DataFrame after addition of new column
    Id    Name  Actual Price  Discount(%)  Final Price
0  302   Watch           300           10        270.0
1  504  Camera           400           15        340.0
2  708   Phone           350            5        332.5
3  103   Shoes           100            0        100.0
4  343  Laptop          1000            2        980.0
5  565     Bed           400            7        372.0 

它通过从 DataFrame 的 Actual Price 一栏中减去折扣额的价值来计算每个产品的最终价格。然后将最终价格值的 Series 分配到 DataFrame items_dfFinal Price 列。

使用 DataFrame.apply() 方法在 Pandas DataFrame 中根据其他列的值创建新列

import pandas as pd

items_df = pd.DataFrame(
    {
        "Id": [302, 504, 708, 103, 343, 565],
        "Name": ["Watch", "Camera", "Phone", "Shoes", "Laptop", "Bed"],
        "Actual_Price": [300, 400, 350, 100, 1000, 400],
        "Discount_Percentage": [10, 15, 5, 0, 2, 7],
    }
)

print("Initial DataFrame:")
print(items_df, "\n")

items_df["Final Price"] = items_df.apply(
    lambda row: row.Actual_Price - ((row.Discount_Percentage / 100) * row.Actual_Price),
    axis=1,
)

print("DataFrame after addition of new column")
print(items_df, "\n")

输出:

Initial DataFrame:
    Id    Name  Actual_Price  Discount_Percentage
0  302   Watch           300                   10
1  504  Camera           400                   15
2  708   Phone           350                    5
3  103   Shoes           100                    0
4  343  Laptop          1000                    2
5  565     Bed           400                    7 

DataFrame after addition of new column
    Id    Name  Actual_Price  Discount_Percentage  Final Price
0  302   Watch           300                   10        270.0
1  504  Camera           400                   15        340.0
2  708   Phone           350                    5        332.5
3  103   Shoes           100                    0        100.0
4  343  Laptop          1000                    2        980.0
5  565     Bed           400                    7        372.0 

它将 apply() 方法中定义的 lambda 函数应用于 DataFrame items_df 的每一行,最后将一系列结果分配到 DataFrame items_dfFinal Price 列。

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

相关文章 - Pandas DataFrame Column