관심 1/Python
데이터프레임 다루기 분포 확인 시각화 보간
give_me_true
2023. 6. 24. 21:45
데이터 프레임에서 컬럼값의 항목들을 확인하고 얼마나 있는지 분포 확인하기
df['col_name'].unique() # 결과는 array 로
df['col_name'].value_counts() # 값들의 분포를 나타냄
데이터 프레임에서 바로 plot 하기
df = pd.DataFrame({'lab':['A','B','C'], 'val':[10,30,20]})
ax = df.plot.bar(x='lab', y='val', rot=0)
df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 0], [7.0, 3.2, 1],
[6.4, 3.2, 1], [5.9, 3.0, 2]],
columns=['length', 'width', 'species'])
ax1 = df.plot.scatter(x='length',y='width', c='DarkBlue')
데이터 프레임 정보 보기
df.numeric.describe()
df.describe(include=[np.number]) # number 만 표현
df.describe(include=[object])
df.describe(include=['O']) # 이렇게도 표현 가능해보임
df.describe(include=['category'])
데이터프레임에서 불필요한 정보 제거하기
- 제거 할 열을 리스트로 선언하여 df.drop(정한list이름, axis=1) 로 제거하기
결측치 보간하기
def df_interpolate(df):
return df.interpolate(method='linear', limit_direction='both', axis=0)
시각화 하기
- plot 하는 함수를 만들어서 여러 그래프 그릴 수 있다
시각화에서 이상치 나타내는 방법
- np.where 로 기준값 보다 큰 범위를 갖는 인덱스를 저장
- iloc 를 이용하여 인덱스들의 리스트를 선택
- 정상 데이터는 plot 으로, 이상치는 scatter 로 표현 가능
ref. https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.bar.html