데이터분석 6기/본캠프

[TIL] 2025.03.18

seyeon1130 2025. 3. 18. 21:06

player_country 결측치 제거

mario_0.dropna(subset=['player_country'], inplace=True)

 

 

그러나 아직도 null값이 있는 submitted_date.

dropna(how = 'any') -> 결측치 있는 행 삭제
dropna(how = 'all') -> 모든 컬럼이 결측치인 행 삭제
dropna()는 행을 삭제하기 때문에 특정 컬럼에 있는 행을 삭제하려면 subset사용해야함.

submitted_date 결측치

submitted_date가 null이지만 검증이 된 데이터들이 있고 그 국가의 종류도 3가지이기 때문에 삭제하지 않는 것으로 결론지으려고 했으나....!!!!

 

튜터님 라이브세션을 들어보니 현업에서는 대부분 결측치는 깔끔하게 삭제하는 것이 바람직하다고 하셨다.

그래서 삭제하는 것으로 결론 내렸다.(이럴거면 그냥 dropna()할 걸..)

 

mario_0.dropna(subset=['submitted_date'], inplace=True)

 

 

 

datetime 타입으로 바꾸기

mario_0['submitted_date']=pd.to_datetime(mario_0['submitted_date'], infer_datetime_format=True)

 

0 star을 가장 많이 하는 국가는?

pd.pivot_table(mario_0,index= 'player_country',values= 'player_id',aggfunc= 'count').sort_values('player_id',ascending=False)

pivot 테이블을 만들어봤을 때, top 3는  United States , Japan, Germany이다.

 

국가별 빈도수 bar

import matplotlib.pyplot as plt

# 각 국가별 플레이어 수를 계산
country_counts = mario_0['player_country'].value_counts()

# 그래프 크기 지정
plt.figure(figsize=(50, 10))

# 막대그래프 그리기
plt.bar(country_counts.index, country_counts.values,color='skyblue')

# 그래프 제목 및 라벨 추가
plt.xlabel('Player Country')
plt.ylabel('Number of Players')
plt.title('Player Count by Country')

# 그래프 출력
plt.show()
values_counts()하면 각 이름별로 카운트가 가능하다. 
index와 value형태인 시리즈로 저장된다.

 

그래프로 보면 이런 느낌!

country_counts = mario_0['player_country'].value_counts().head()

 

top5만 보면 이런 느낌!

리더보드 순위 Scatter

 

과연 실력도 같을지?!

 

 

여기서 알 수 있는 점은, 빈도수가 적은 국가일 수록 순위가 낮다.

 

selected_countries = ['United States', 'Japan', 'Germany','France','Canada']
mario_0_select = mario_0[mario_0['player_country'].isin(selected_countries)]
plt.scatter(mario_0_select['player_country'],mario_0_select['place'])

 

top5는 순위도 크게 차이가 나지 않는다.

canada는 빈도수도 5위인데다 1위 보유 국가이다.

 

plt.scatter(mario_0['place'],mario_0['primary_time_seconds'])
plt.xlabel('place')
plt.ylabel('time')

시간은 300위까지는 큰 차이가 없다가 300위부터 크게 늘어나는 경향을 볼 수 있다.

 

가장 많이 사용하는 플랫폼

plat = mario_0['platform'].value_counts()

plt.bar(plat.index,plat.values)
plt.xlabel('platform')
plt.ylabel('count')
plt.show()

 

platform_pivot = mario_0.pivot_table(index='player_country', columns='platform', aggfunc='size', fill_value=0)
platform_pivot.plot(kind='bar', stacked=False, figsize=(12, 6), colormap='tab10')

plt.xlabel("Country")
plt.ylabel("Platform Usage Count")
plt.title("Most Used Platforms by Country")
plt.xticks(rotation=90)
plt.legend(title="Platform")
plt.show()
count vs size
피봇테이블에서 value가 있으면 count, 없으면 size
size는 null값을 포함해서 갯수를 세기 때문에 fill_value =0 해줘야함

stacked 
True : 누적 막대
False : 개별 그룹 막대

xticks(rotation =90) : 글자가 너무 길 때 세로로 출력하도록 사용
legend : 막대 이름 나옴