반응형
SQL과 pandas 문법으로 각각 같은 데이터를 출력해보자
1. 전체 player_id, 유니크 player_id
SQL - COUNT(column), COUNT(Distinct column)
SELECT count(player_id), count(distinct player_id)
FROM table_name
Pandas - colums.count(), column.nunique()
df_all['player_id'].count() #count
df_all['player_id'].nunique() #unique count
2. id별 Game 횟수
* row 1개가 game 1회에 대한 정보를 담고 있음
SQL - GROUP BY
SELECT player_id, count(*) AS '게임횟수'
FROM table_name
GROUP BY player_id
Pandas - groupby.size()
df_all.groupby('player_id').size().to_frame('게임횟수').sort_values('게임횟수',ascending=False)
3. player_id에서 team 명 추출하기
* player_id가 team명_playername 으로 구성되어 있음
ex. 팀명: DATA, 이름: hana -> player_id : DATA_hana
SQL - substring_index(string , ' 구분값' , 인덱스)
SELECT player_id, substring_index(player_id,'_',1) AS Team
FROM esports_matches_summary_stats
GROUP BY player_id
Pandas - df.columns.split('구분값')
df_all['player_id'].str.split('_',expand=True)[0])
같은 데이터를 출력하는데에 논리적인 로직은 비슷하지만, SQL과 Pandas의 표현 방법은 다르다.
그래도 하나를 배우면 나머지 한쪽에서는 좀 더 빠르게 적용해볼 수 있는것 같다.
반응형
'Today I Learned > SQL' 카테고리의 다른 글
postgresql json data로 변환하고 테이블로 만들어서 사용하기 (0) | 2023.07.13 |
---|---|
mac postgreSQL pgenv로 설치하기 + 기본 실습 (0) | 2023.07.05 |
[SQL] 대소문자 구분하기 - Binary (0) | 2020.10.28 |
[Bigquery] Time Data 다루기 (0) | 2020.09.29 |
subquery 를 From 절에 쓸 때 주의할 점 (0) | 2020.08.31 |