创建时间: January-22, 2021 | 更新时间: February-09, 2021
本教程将介绍我们如何在 Pandas DataFrame 中根据 DataFrame 中其他列的值,通过对列的每个元素应用函数或使用 DataFrame.apply() 方法来创建新的列。
pythonCopy`import pandas as pditems_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)`
输出:
textCopy `Id Name Cost Discount(%)0 302 Watch 300 101 504 Camera 400 152 708 Phone 350 53 103 Shoes 100 04 343 Laptop 1000 25 565 Bed 400 7`
我们将使用上面代码片段中显示的 DataFrame 来演示如何根据 DataFrame 中其他列的值在 Pandas DataFrame 中创建新的列。
Pandas DataFrame 中根据其他列的值按元素操作创建新列
Video Player is loading.
Current Time 0:00
Duration 50:27
Remaining Time 50:27
pythonCopy`import pandas as pditems_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")`
输出:
textCopy`Initial DataFrame:Id Name Actual Price Discount(%)0 302 Watch 300 101 504 Camera 400 152 708 Phone 350 53 103 Shoes 100 04 343 Laptop 1000 25 565 Bed 400 7DataFrame after addition of new columnId Name Actual Price Discount(%) Final Price0 302 Watch 300 10 270.01 504 Camera 400 15 340.02 708 Phone 350 5 332.53 103 Shoes 100 0 100.04 343 Laptop 1000 2 980.05 565 Bed 400 7 372.0`
它通过从 DataFrame 的 Actual Price 一栏中减去折扣额的价值来计算每个产品的最终价格。然后将最终价格值的 Series 分配到 DataFrame items_df 的 Final Price 列。
使用 DataFrame.apply() 方法在 Pandas DataFrame 中根据其他列的值创建新列
pythonCopy`import pandas as pditems_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")`
输出:
textCopy`Initial DataFrame:Id Name Actual_Price Discount_Percentage0 302 Watch 300 101 504 Camera 400 152 708 Phone 350 53 103 Shoes 100 04 343 Laptop 1000 25 565 Bed 400 7DataFrame after addition of new columnId Name Actual_Price Discount_Percentage Final Price0 302 Watch 300 10 270.01 504 Camera 400 15 340.02 708 Phone 350 5 332.53 103 Shoes 100 0 100.04 343 Laptop 1000 2 980.05 565 Bed 400 7 372.0`
它将 apply() 方法中定义的 lambda 函数应用于 DataFrame items_df 的每一行,最后将一系列结果分配到 DataFrame items_df 的 Final Price 列。
相关文章 - Pandas DataFrame Column
- 如何将 Pandas DataFrame 列标题获取为列表
- 如何删除 Pandas DataFrame 列
- 如何在 Pandas 中将 DataFrame 列转换为日期时间
- 如何获得 Pandas 列中元素总和

https://www.delftstack.com/zh/howto/python-pandas/pandas-create-column-based-on-other-columns/
