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
列。
相关文章
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 系列日期时间转换为字符串