공부/Microsoft Data School 1기

파이썬으로 데이터 수집, 형태 및 분석 2

_빌런 2025. 4. 15. 09:00

pandas

출처: https://wikidocs.net/205999

 

pandas는 Series와 DataFrame이라는 두 가지 자료구조를 제공한다.

Series는 순차 데이터를 저장하는 1차원 배열로, 단일 타입(숫자 또는 문자) 데이터로 구성한다.

DataFrame은 행열로 구성된 2차월 배열로, 각 열에 서로 다른 타입의 Series로 구성한다.

 

Series의 구조 설명은 위와 같다.

 

Series 기본 속성 설명
values  
index  
dtype  
name  
shape  

pandas 라이브러리의 Series 생성자에 대한 설명이다.

자세한 것은 공식 docs의 Series 설명을 참고하도록 하자.

 

Series 메소드 설명
info()  
value_counts()  
sort_values()  
isnull()  
notnull()  
dropna()  
fillna()  
astype()  
agg func(집계 함수) mean() 평균 / sum() 합계 / min() 최솟값 / max() 최댓값 / std() 표준편차 등

pandas 라이브러리의 Series 생성자의 기본 메소드들이다.

자세한 것은 공식 docs의 Series 설명을 참고하도록 하자.

 

Series 데이터 관리 구분 설명
series.loc[] 조회 index label로 접근하는 방법이다. list나 slice로 접근 시 마지막 index를 포함한다.
series.iloc[] 조회 range index label로 접근하는 방법이다. list나 slice로 접근 시 마지막 index를 미포함한다.
series[index] = data 추가, 수정 index label을 key로 해서 value를 data로 변경한다.
del series[index] 삭제 index label을 key로 해서 series의 index-value 쌍을 제거한다.

pandas 라이브러리의 Series 생성자의 데이터 관련 함수들이다.

데이터 조회, 추가, 수정, 삭제 등에 대한 함수다.

 

DataFrame의 구조 설명은 위와 같다.

 

DataFrame 메소드 설명
shape  
dtypes  
columns  
values  
index  
size  
T  
describe  
agg func(집계 함수) mean() 평균 / sum() 합계 / min() 최솟값 / max() 최댓값 / std() 표준편차 등
value_counts  
sort_values(
    by=col_name,
    ascending=bool
)
col_name을 기준으로 정렬하는 함수
ascending 옵션으로 True/False를 지정하여 오름차순/내림차순을 지정할 수 있다.
sort_index()  

pandas 라이브러리의 DataFrame 생성자에 대한 메소드 설명이다.

자세한 것은 공식 docs의 DataFrame 설명을 참고하도록 하자.

 

DataFrame Index 설명
df.reset_index() index label을 range index로 초기화하고,
현재 index label 값을 index 컬럼으로 데이터 프레임에 들어간다.
df.set_index(column) 현재 존재하는 column을 index label로 설정한다.
column에 list로 값을 주어 중첩 index를 설정하는 것도 가능하다.
df.rename(mapper, *index, *axis) mapper(dict거나 함수)로 주어진 대로 이름을 변경한다.

pandas 라이브러리의 DataFrame의 index 관련 함수에 대한 설명이다.

자세한 것은 공식 docs의 DataFrame 설명을 참고하도록 하자.

 

DataFrame 데이터 관리 구분 설명
info 조회 메소드 객체 자체를 조회한다.
info() 조회 info 메소드로, DataFrame의 요약 정보를 출력한다.
head(n) 조회 데이터프레임의 상위 n개 행을 샘플링하여 보여준다.
tail(n) 조회 데이터프레임의 하위 n개 행을 샘플링하여 보여준다.
sample(n) 조회 데이터프레임의 무작위 n개 행을 샘플링하여 보여준다.
loc[:] 조회 index label로 접근하는 방법이다. list나 slice로 접근 시 마지막 index를 포함한다.
iloc[:] 조회 range index label로 접근하는 방법이다. list나 slice로 접근 시 마지막 index를 미포함한다.
df[col_name] 조회 Series 객체로 데이터를 조회한다. col_name을 list로 던져줄 수 있다.
df[[col_name]] 조회 DataFrame 객체로 데이터를 조회한다. col_name을 list로 던져줄 수 있다.
df[col_name] = data 추가, 수정 col_name의 컬럼에 데이터를 추가한다. 이미 컬럼이 있다면 수정한다.
drop(col_name, axis) 삭제 axis에 해당하는 col_name 컬럼을 삭제한다.

pandas 라이브러리의 DataFrame의 데이터 관련 함수들이다.

데이터 조회, 추가, 수정, 삭제 등에 대한 함수다.

 

DataFrame 결측치 함수 설명
isna() / isnull()  
dropna  
fillna  
ffill  
bfill  

pandas 라이브러리의 DataFrame의 결측치 관련 함수들이다.

 

Visualization

그래프 종류 설명
plot  
bar  
barh  
pie  
scatter  
hist  
boxplot  
violinplot  

dd