728x90
테이블 생성코드
data = [[13, 15, 30], [10, 20, 15]]
triangle = pd.DataFrame(data, columns=['x', 'y', 'z']).astype({'x':'Int64', 'y':'Int64', 'z':'Int64'})
문제

풀이코드
import pandas as pd
def triangle_judgement(triangle: pd.DataFrame) -> pd.DataFrame:
triangle['Triangle'] = ['Yes' if (x+y>z) and (x+z>y) and (z+y>x) else 'No'
for x,y,z in zip(triangle['x'],triangle['y'],triangle['z'])]
return triangle
해설
1. 생성할때는 없던 Triangle 컬럼을 만들어야한다
triangle['Triangle'] = ~~~ 구조를 통해서, ~~~에 해당하는 값을 triangle['Triangle']에 넣어줄 수 있다 ↓
2. ['Yes' if (x+y>z) and (x+z>y) and (z+y>x) else 'No' for x,y,z in zip(triangle['x'],triangle['y'],triangle['z'])]
위의 코드를 해설하면 triangle['x'] ,triangle['y'] ,triangle['z'] 각각의 컬럼의 값을 x, y, z 에 zip 함수로 하나씩 넣어주고
그 x, y, z 값을 이용해 만약 (x+y>z) and (x+z>y) and (z+y>x) 이라면 'Yes' 값을, 아니라면(=else) 'No'를 입력한다.
3. 따라서 triangle['Triangle'] 값에는 'No' 와 'Yes'의 값이 조건에 만족하는지 여부에 따라 넣어지게되고
triangle을 실행하면 원래 x,y,z 컬럼이 있었으므로 추가된 Triangle 컬럼까지가 출력되게 된다.
728x90
'• data analysis > PYTHON' 카테고리의 다른 글
1667. Fix Names in a Table (0) | 2024.02.27 |
---|---|
1075. Project Employees I (0) | 2024.02.26 |
1068. Product Sales Analysis I (0) | 2024.02.22 |
[leetcode] 1148. Article Views I (0) | 2024.02.20 |
[leetcode] 620. Not Boring Movies (0) | 2024.02.19 |