Pandas는 파이썬 데이터 분석 라이브러리이다.
3가지의 데이터 구조를 제공하며, Series(1차원), DataFrame (2차원) ,Panel(3차원)이 있다.
그 중 DataFrame은 행(rows)과 열(columns)로 이루어진 데이터 구조이다.
- df.info() : 데이터에 대한 간략한 걸명과 특히 전체 행(rows) 수, 각 특성의 데이터 타입과 null이 아닌 값의 개수를 확인 하는데 유용하다.
- df.drop("columns_name",axis =1) : 특정 column을 삭제 한다.
- df.head() : 처음 다섯행을 확인 할 수 있다.
- df["columns_name"].value_counts() : 특정 column의 카테고리의 파악 및 수량을 확인 하는 메서드
value_counts(dropna = False)를 사용하면 NaN값도 확인 가능 하다. - df.describe() : 숫자형 특성의 요약 정보를 보여준다. count,mean,std,min,25%,50%,75%,max
(25,50,75% 백분위수를 의미 - 전체 관측값에서 주어진 백분율이 속하는 하위 부분의 값을 나타냄) - df.hist(bins = 50,figsize=(20,15)) : histogram을 그려주는 메서드 (matplotlib.pyplot import 해줘야함) 데이터를 한번에 파악 가능, 추가적으로 주피터에서 사용할 경우 매직메서드인 (%matplotlib inline)을 해줘야함
- df.reset_index() : index열이 추가 된 데이터 프레임이 반환된다.
- df.corr() : 특정 column과 다른 columns의 표준 상관계수를 구할 수 있다.(데이터셋이 너무 크면 사용을 지양하자.)
corr_matrix = df.corr()
corr_matrix["columns_name"].sort_values(ascending = False)
- df.plot(kind = "scatter",x="columns_1",y = "columns_2",alpha=0.1) : column 두개로 두 column의 연관관계 등을 그래프로 확인 가능하다. kind 는 그래프 종류 , x,y는 각각의 축을 의미한다. 추가적으로 alpha 옵션을 주면 밀집된 영역을 표현할 수 있다. 다양한 옵션이 추가적으로 있다.
공식 사이트 : https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.html
pandas.DataFrame.plot — pandas 1.5.2 documentation
sequence of iterables of column labels: Create a subplot for each group of columns. For example [(‘a’, ‘c’), (‘b’, ‘d’)] will create 2 subplots: one with columns ‘a’ and ‘c’, and one with columns ‘b’ and ‘d’. Remaining colum
pandas.pydata.org
추가적인 함수
scatter_matrix : list로 묶은 columns의 상관관계를 그래프로 파악할 수 있다.
from pandas.plotting import scatter_matrix
columns_list = ["columns_01","columns_02","columns_03","columns_04"]
scatter_matrix(df[columns_list],figsize = (12,8))
# reference
refer 1. Hands-On Machine Learning with Scikit-Learn,Keras&TensorFlow
image1. https://www.w3resource.com/python-exercises/pandas/index-dataframe.php