무냐의 개발일지

627. Swap Salary 본문

LeetCode 코딩테스트

627. Swap Salary

무냐코드 2024. 5. 9. 17:52

👩‍💻 문제

 

Write a solution to swap all 'f' and 'm' values (i.e., change all 'f' values to 'm' and vice versa) with a single update statement and no intermediate temporary tables. Note that you must write a single update statement, do not write any select statement for this problem. The result format is in the following example.

Example 1:
Input: 
Salary table:
+----+------+-----+--------+
| id | name | sex | salary |
+----+------+-----+--------+
| 1  | A    | m   | 2500   |
| 2  | B    | f   | 1500   |
| 3  | C    | m   | 5500   |
| 4  | D    | f   | 500    |
+----+------+-----+--------+
Output: 
+----+------+-----+--------+
| id | name | sex | salary |
+----+------+-----+--------+
| 1  | A    | f   | 2500   |
| 2  | B    | m   | 1500   |
| 3  | C    | f   | 5500   |
| 4  | D    | m   | 500    |
+----+------+-----+--------+

 

💡 풀이

import pandas as pd

def swap_salary(salary: pd.DataFrame) -> pd.DataFrame:
    salary.update(salary['sex'].map(lambda x : 'm' if x=='f' else 'f'))
    return salary

 

✍️ 해설

update 기능을 몰랐다 ...? 세상에나

map 안에 lambda 처리로 m은 f로 바꾸고, f는 m으로 바꾸는 처리를 해줄 수 있다

'LeetCode 코딩테스트' 카테고리의 다른 글

Stack : sort_stack 해설  (0) 2024.06.17
LL: Reverse Between 해설  (0) 2024.06.14
1791. Find Center of Star GraphS  (0) 2024.05.09
1920. Build Array from Permutation  (0) 2024.04.15
2859. Sum of Values at Indices With K Set Bits  (0) 2024.04.14