[Study]/[Pandas]

[LeetCode - Pandas] (Easy) 2878. Get the Size of a DataFrame

잰잰' 2025. 4. 2. 17:19
DataFrame players:
+-------------+--------+
| Column Name | Type   |
+-------------+--------+
| player_id   | int    |
| name        | object |
| age         | int    |
| position    | object |
| ...         | ...    |
+-------------+--------+

해결책 작성: 선수들의 행과 열의 수를 계산하여 표시하세요.

결과는 배열 형식으로 반환하세요:

[행의 수, 열의 수]

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

 

Example 1:
Input:
+-----------+----------+-----+-------------+--------------------+
| player_id | name     | age | position    | team               |
+-----------+----------+-----+-------------+--------------------+
| 846       | Mason    | 21  | Forward     | RealMadrid         |
| 749       | Riley    | 30  | Winger      | Barcelona          |
| 155       | Bob      | 28  | Striker     | ManchesterUnited   |
| 583       | Isabella | 32  | Goalkeeper  | Liverpool          |
| 388       | Zachary  | 24  | Midfielder  | BayernMunich       |
| 883       | Ava      | 23  | Defender    | Chelsea            |
| 355       | Violet   | 18  | Striker     | Juventus           |
| 247       | Thomas   | 27  | Striker     | ParisSaint-Germain |
| 761       | Jack     | 33  | Midfielder  | ManchesterCity     |
| 642       | Charlie  | 36  | Center-back | Arsenal            |
+-----------+----------+-----+-------------+--------------------+
Output:
[10, 5]

설명: 이 DataFrame은 10개의 행과 5개의 열을 포함합니다.

 

✏️ 풀이

# 풀이1 #
import pandas as pd

def getDataframeSize(players: pd.DataFrame) -> List[int]:
    rows, columns = players.shape

    return [rows, columns]
    
# 풀이2 #
import pandas as pd

def getDataframeSize(players: pd.DataFrame) -> List[int]:
    return [players.shape[0], players.shape[1]]
    
# 풀이3 #
import pandas as pd

def getDataframeSize(players: pd.DataFrame) -> List[int]:
    return list(players.shape)

 

📚 개념정리

shape() ? 데이터프레임의 크기를 나타내는 속성으로 행 수와 열 수를 튜플로 반환

df.shape