DataFrame employees
±------------±-------+
| Column Name | Type. |
±------------±-------+
| name | object |
| salary | int. |
±------------±-------+
一家公司计划为员工提供奖金。
编写一个解决方案,创建一个名为 bonus 的新列,其中包含 salary 值的 两倍。
返回结果格式如下示例所示。
示例 1:
输入:
DataFrame employees
±--------±-------+
| name | salary |
±--------±-------+
| Piper | 4548 |
| Grace | 28150 |
| Georgia | 1103 |
| Willow | 6593 |
| Finn | 74576 |
| Thomas | 24433 |
±--------±-------+
输出:
±--------±-------±-------+
| name | salary | bonus |
±--------±-------±-------+
| Piper | 4548 | 9096 |
| Grace | 28150 | 56300 |
| Georgia | 1103 | 2206 |
| Willow | 593 | 13186 |
| Finn | 74576 | 149152 |
| Thomas | 24433 | 48866 |
±--------±-------±-------+
解释:
通过将 salary 列中的值加倍创建了一个新的 bonus 列。
直接将DataFrame的bonus列赋值为两倍的salary列值即可:
import pandas as pd
def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame:
employees['bonus'] = employees['salary'] * 2
return employees
或者用DataFrame的apply+lambda:
import pandas as pd
def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame:
employees['bonus'] = employees['salary'].apply(lambda x : 2 * x)
return employees