Leetcode_30 Days of Pandas
문제 출처
https://leetcode.com/studyplan/30-days-of-pandas/
1. 문제
"Fix Names in a Table" / 네임 테이블 수정하기
Table : Users
Coulumn Name | Type |
user_id | int |
name | varchar |
Write a solution to fix the names so that only the first character is uppercase and the rest are lowercase.
Return the result table ordered by user_id.
The result format is in the following example.
input |
► |
output |
||
user_id | name | user_id | name | |
1 | aLice | 1 | Alice | |
2 | bOB | 2 | Bob |
CheckPoint
1. 첫번째 문자 대문자로 출력
2. user_id로 반환하하는데 오름차순으로 출력
2. 문제풀이
1차시. 정답
1. users 테이블에서 문자 capitalize() 문법 사용
*capitalize()
- 함수는 파이썬에서 문자열의 첫 글자를 대문자로 변환하고, 나머지 글자들을 소문자로 변환하는 메소드
- 이 메소드는 특별히 인자를 필요로 하지 않으며, 문자열의 첫 번째 글자만 대문자로 변환
2. result_df로 묶어주고 users테이블에서 'user_id'로 오름차순으로 return
import pandas as pd
def fix_names(users: pd.DataFrame) -> pd.DataFrame:
users['name'] = users['name'].str.capitalize()
result_df = users.sort_values(by='user_id', ascending = True)
return result_df
'Python > LeetCode' 카테고리의 다른 글
[LeetCode] String Methods 1527. Patients With a Condition (0) | 2024.01.03 |
---|---|
[LeetCode] String Methods 1517. Find Users With Valid E-Mails (0) | 2024.01.03 |