코딩 테스트/Pandas

Rising Temperature

susinlee 2024. 12. 21. 10:02

https://leetcode.com/problems/rising-temperature/description/

 

[문제] 

Write a solution to find all dates' id with higher temperatures compared to its previous dates (yesterday).

Return the result table in any order.

The result format is in the following example.

 

1. diff() 함수는 연속된 행의 값의 차이를 계산하는 함수. 즉 이전 행과의 현재 행의 차이를 계산

→ 현재행 값 - 이전행 값

 

Pandas

import pandas as pd

def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame:
    weather.sort_values(by='recordDate', inplace=True)

    condition1 = weather.recordDate.diff().dt.days == 1
    condition2 = weather.temperature.diff() > 0

    return weather[condition1 & condition2][['id']]

 

SQL

SELECT w1.id
FROM weather w1, weather w2
WHERE DATEDIFF(w1.recordDate, w2.recordDate) = 1
AND w1.temperature > w2.temperature

SELECT w1.id
FROM weather w1
JOIN weather w2
	ON w1.recordDate = DATE_ADD(w2.recordDate, INTERVAL 1 DAY)
WHERE w1.temperature > w2.temperature

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

Employee Bonus  (0) 2024.12.22
Average Time of Process per Machine  (0) 2024.12.21
Customer Who Visited but Did Not Make Any Transcations  (1) 2024.12.20
Product Sales Analysis 1  (0) 2024.12.20
대충 만든 자판  (0) 2024.12.19