본문 바로가기

자격증공부/빅데이터분석기사

[빅데이터분석기사] 작업형1 문제유형 (기초통계, 그룹통계, 날짜)

320x100

빅데이터분석기사

* 퇴근후딴짓 님의 강의를 참고하였습니다. *

[문제 1] index '2001' 데이터(행)의 평균보다 큰 값의 수와 index'2003' 데이터(행)의 평균보다 작은 값의 수를 더하시오.

df = pd.read_csv("data.csv", index_col="Unnamed: 0")
# print(df.head(2))

m2001 = df.loc[2001].mean()
# print(m2001)
cond = df.loc[2001] > m2001
r1 = sum(cond) #True인것만 합해서 구해줌

m2003 = df.loc[2003].mean()
cond = df.loc[2003] < m2003
r2 = sum(cond)

print(r1+r2)

 

빅데이터분석기사

320x100

[문제 2] 결측값을 가진 데이터는 바로 뒤에 있는 값으로 대체한 후 city와 f2컬럼 기준으로 그룹합을 계산한 뒤 veiws가 세번쨰로 큰 city 이름은?

* 결측치 뒤에 값으로 채우는 경우 fillna(method='bfill') / 앞에 값으로 채우는 경우 fillna(method='ffill')

import pandas as pd
df = pd.read_csv('members.csv')

df.isnull().sum()
display(df.head())
df = df.fillna(method='bfill') # 뒤에 값으로 채우고 싶을 떄는 bfill / 앞에 값으로 채우고 싶을 경우 ffill
df.isnull().sum()
display(df.head())

df.groupby(['city','f2']).sum()

# sorting하려면 풀어줘야 함
df = df.groupby(['city','f2']).sum().reset_index() # index값 reset
df = df.sort_values('views', ascending = False)
print(df.iloc[2,0])
반응형

[문제 3] 구독(subscribed) 월별로 데이터 개수를 구한 뒤, 가장 작은 구독수가 있는 월을 구하시오.

* 날짜형식 데이터 타입으로 변경 : to_datetime(df['컬럼명'])

import pandas as pd
df = pd.read_csv('members.csv')
df.head()
df.info()

df['subscribed'] = pd.to_datetime(df['subscribed'])
df.info()
df['year'] = df['subscribed'].dt.year
df['month'] = df['subscribed'].dt.month
df['day'] = df['subscribed'].dt.day

df = df.groupby('month').count()
print(df.sort_values('subscribed').index[0])

빅데이터분석기사

320x100
반응형