pandas dataframe 基于其他列创建新列-ag捕鱼王app官网

pandas dataframe 基于其他列创建新列

作者:迹忆客 最近更新:2024/04/23 浏览次数:

本教程将介绍我们如何在 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 列。

上一篇:dataframe 获取给定列的第一行

下一篇:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:python

pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。

pandas 追加数据到 csv 中

发布时间:2024/04/24 浏览次数:352 分类:python

本教程演示了如何在追加模式下使用 to_csv()向现有的 csv 文件添加数据。

pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:python

本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。

pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:python

本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图