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_df
的 final 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_df
的 final price
列。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
pandas dataframe dataframe.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:python
-
dataframe.shift() 函数是将 dataframe 的索引按指定的周期数进行移位。
python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:python
-
python pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:python
-
pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。
pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:python
-
本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。
pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:python
-
本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。
在 python 中将 pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:python
-
了解如何在 python 中将 pandas 系列日期时间转换为字符串