[Study]/[Pandas]

[LeetCode - Pandas] (Easy) 2881. Create a New Column

잰잰' 2025. 4. 1. 17:19
DataFrame employees
+-------------+--------+
| Column Name | Type.  |
+-------------+--------+
| name        | object |
| salary      | int.   |
+-------------+--------+

회사는 직원들에게 보너스를 제공할 계획입니다.

급여 컬럼의 값을 두 배로 만든 새로운 컬럼 이름 bonus를 생성하는 해결책을 작성하세요.

결과 형식은 아래 예시와 같습니다.

 

Example 1:
Input:
DataFrame employees
+---------+--------+
| name    | salary |
+---------+--------+
| Piper   | 4548   |
| Grace   | 28150  |
| Georgia | 1103   |
| Willow  | 6593   |
| Finn    | 74576  |
| Thomas  | 24433  |
+---------+--------+
Output:
+---------+--------+--------+
| name    | salary | bonus  |
+---------+--------+--------+
| Piper   | 4548   | 9096   |
| Grace   | 28150  | 56300  |
| Georgia | 1103   | 2206   |
| Willow  | 6593   | 13186  |
| Finn    | 74576  | 149152 |
| Thomas  | 24433  | 48866  |
+---------+--------+--------+

설명 : 새로운 컬럼 bonus는 salary 컬럼의 값을 두 배로 만들어 생성됩니다.

 

✏️ 풀이

import pandas as pd

def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame:
    employees['bonus'] = employees['salary'] * 2

    return employees