데이터 처리 기초 내용 정리

ssssssssh·2024년 5월 27일

이것은 단순히 퀴즈 준비.
데이터 전처리 과정을 조금 정리해본다.

데이터 전처리 (가공 = 핸들링 = 랭글링 = 먼징 = 클리닝)

  • query () : 행 추출
#1반인 경우
nclass == 1
#1반 + 수학 점수 50점 이상
nclass == 1 & math >= 50
#영어 90점 초과 이거나 과학 50점 미만
english < 90 | science < 50
#반이 1, 3, 5에 해당되는 경우
nclass in [1,3,5]
#데이터 값이 문자
sex == "F"
  • df [] : 열 (컬럼 / 변수) 추출
#변수 제거
df.drop(columns = '')
#데이터 빈도수 출력
df['class'].value_counts()
-> 반의 개수가 출력됨
  • sort_values() : 정렬
#오름차순
df.sort_values('', ascending=True)
#내림차순
df.sort_values('', ascending=False)
  • assign() : 변수 추가
#파생변수 추가
df.assign(total = 
exam['math'] + exam['english'] + exam['science']

집단 별 요약

→ df.groupby(’key’).agg(sum_data = (’data’, ‘sum’))

  • groupby() : 집단별로 나누기
    • subset = [’’] : 특정한 컬럼만 지정
  • agg() : 집단 별 통계치 구하기
#반 별 그룹화, 그룹 별 math 평균 구하고 해당 변수 : mean_math 지정
df.groupby('nclass', as_index = False)
.agg(mean_math = ('math', 'mean'))
  • merge() : 데이터 합치기 (열 / 가로)
total =
pd.merge(test1, test2, how = 'right', on = 'id')
#how = 'right' / 'left' -> 조인 방향
  • concat() : 데이터 합치기 (행 / 세로)
group_all =
pd.concat([group_a, group_b, ignore_index=True)
#ignore_index = True : 인덱스 값 초기화
  • 결측치
#결측치 확인
df.isna()
df.isna().sum() #결측치 개수
#결측치 제거
df.dropna()
  • 결측치 처리
#평균 대입
mean_value = df['math'].mean()
df['math'] = df['math'].fillna(mean_value)
  • 극단치 관련 처리
#1사분위수 / 3사분위수
pct25 = df['score'].quantile(.25)
pct75 = df['score'].quantile(.75)

#IQR
iqr = pct75 - pct25

#하한, 상한
pct25 - 1.5 * iqr
pct75 + 1.5 * iqr
  • 파이썬 기본 그래프 sns.lineplot : 막대그래프 sns.scatterplot : 산점도

기회가 된다면...
추후 업로드 시 더 자세히 많은 내용을 정리해보는 걸로 하겠다.

profile
Security | Cloud | AI

0개의 댓글