df.rename(columns={'분양가격(㎡)':'분양가격'})
# 분양가격의 dtype을 int64로 변경하기
df['분양가격'].astype(int)
# ' ' 공백이 존재하는 데이터가 있는지 확인
df.loc[df['분양가격'] == ' ']
# strip()를 사용하여 공백이 있는 데이터의 해당 앞뒤 공백을 삭제
df['분양가격'].str.strip()
# 데이터가 없는 경우 0으로 변경
df.loc[df['분양가격'] == '', '분양가격'] = 0
# 콤마를 제거
df['분양가격'].str.replace(',','')
# NaN을 0으로 변환
df['분양가격'].fillna(0)
df['규모구분'].value_counts()
# 지역명별로 평균 분양가격을 확인
df.groupby('지역명')['분양가격'].mean()
# 작업파일 저장
df.to_csv('ds-house-price-clean.csv', index=False)
matplotlib
!pip install matplotlib
import matplotlib.pyplot as plt
!sudo apt-get install -y fonts-nanum
!sudo fc-cache -fv
!rm ~/.cache/matplotlib -rf
# 상단 메뉴 '런타임'에서 '다시 시작 및 모두실행' 클릭
plt.rc('font',family='NanumBarunGothic')
arange(x,y)
data1 = np.arange(1,50) # 1부터 50까지 상승하는 직선 생성
plt.plot(data1) # plt에 데이터 삽입
data2 = np.arange(50,100) # 50부터 100까지 상승하는 직선 생성
plt.plot(data2) # plt에 데이터 삽입
plt.show() # 그래프 보기
subplot(row, column, no)
data1 = np.arange(100,200)
plt.subplot(2, 1, 1) # 2열 1행
plt.plot(data1)
data2 = np.arange(200,300)
plt.subplot(2, 1, 2) # 2열 1행
plt.plot(data2)
------------------------------------------------------------------------
# 1행 3열 그래프
data1 = np.arange(0, 100)
plt.subplot(1, 3, 1) # plt.subplot(131)
plt.plot(data1)
data2 = np.arange(0,100)
plt.subplot(1, 3, 2) # plt.subplot(132)
plt.plot(data2)
data2 = np.arange(0,100)
plt.subplot(1, 3, 3) # plt.subplot(133)
plt.plot(data2)
스타일 옵션
plt.title('타이틀 테스트', fontsize= 30)
plt.xlabel('x축', fontsize=15)
plt.ylabel('y축', fontsize=15, rotation=0)
marker: 선의 핵심표시
ex) *, v, o
color: 색깔
ex) b, r, y, #000000
linestyle: 선 스타일
ex) ‘-’, ‘-.’, ‘’
alpha: 투명도 설정
plt.plot(np.arange(10), np.arange(10)*3, marker='*', color='y', linestyle='-.', alpha=0.1)
x = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Korean']
y = [60, 80, 50, 90, 20, 70]
alpha: 투명도
align: edge(컬럼명 왼쪽정렬), center
color: 바 색깔
plt.bar(x, y, align='edge', alpha=0.7, color='red')
xlabel
ylabel
plt.xlabel('score of Students')
plt.ylabel('score of Students')
axis
- on: 축과 라벨을 켬
- off: 축과 라벨을 끔
plt.axis('off')
imshow(리스트)
- interpolation: 픽셀들의 축 위치 간격을 보정하여 이미지가 자연스러운 모양으로 보일 수 있게 하는 방법(보간법)
plt.imshow(wordcloud, interpolation='kaiser')
retail['InvoiceDate'] = pd.to_datetime(retail['InvoiceDate'])
# 국가별 매출금액을 구하여 오름차순 정렬
retail.groupby('Country').sum()['CheckoutPrice'].sort_values()
NaN 값이 아닌 값만 뽑을 수 있음
retail[pd.notnull(retail['CustomerID'])]
# 구입 수량이 1이상인 데이터만 제장
retail = retail[retail['Quantity'] > 0
def extract_month(date): # 2011-12-09 12:50:00 -> 201112
month = str(date.month)
if date.month < 10:
month = '0' + month
return str(date.year) + month
retail.set_index('InvoiceDate').groupby(extract_month).sum()['CheckoutPrice']