# 필요모듈 import
# numpy, pandas 사용
import numpy as np
import pandas as pd
# 숫자값의 콤마를 문자로인식될수 있기때문에
# 천단위구분(thousands=",")이라고 알려주고 콤마를제거 숫자형으로 읽는다
thousands=",", encoding="euc-kr"
# 엑셀 불러오기 안될경우 (openpyxl 설치)
!pip install openpyxl
# 엑셀 불러오기
df = pd.read_excel("../data/02. sales-funnel.xlsx")
pivot_table() --> (괄호)안의 정보들로 표를 만들어라
# Name 컬럼을 인덱스로 설정
# pd.pivot_table(df, index="Name")
df.pivot_table(index="Name")
# 멀티 인덱스 설정
df.pivot_table(index=["Manager", "Rep"])
df.pivot_table(index=["Name", "Rep", "Manager"])
# values=Price 컬럼 sum 연산 적용
df.pivot_table(index=["Manager", "Rep"], values="Price", aggfunc=np.sum)
# values=Price 컬럼 len 갯수 적용
df.pivot_table(index=["Manager", "Rep"], values="Price", aggfunc=[np.sum, len])
# product를 컬럼으로 지정
df.pivot_table(index=["Manager", "Rep"], values="Price", columns="Product", aggfunc=np.sum)
# Nan 값 설정 : fill_value
df.pivot_table(index=["Manager", "Rep"], values="Price", columns="Product", aggfunc=np.sum, fill_value=0)
# index, values, aggfunc 2개 이상 설정
df.pivot_table(index=["Manager", "Rep", "Product"],
values=["Price", "Quantity"],
aggfunc=[np.sum, np.mean],
fill_value=0,
margins=True) # 총계(All) 추가
# Multiindex
crime_station.columns
# sum, 건수, 강도, 검거 컬럼 자료 확인
crime_station["sum", "건수", "강도", "검거"][:5]
# 다중컬럼에서 특정 컬럼 제거 (컬럼[0]자리 [1]자리 제거)
crime_station.columns = crime_station.columns.droplevel([0, 1])
crime_station.columns
pip install googlemaps
(google maps API Key 필요)
서울영등포경찰서 정보 불러오기
코드, 언어
pd.read_csv() --> (괄호)안의 파일 읽기
변수명["컬럼"].unique() --> 특정컬럼의 unique조사(결과:array([자료, 자료, 자료])
pd.pivot_table() 내부에 사용가능 언어
index = ["Manager", "Rep"] --> 인덱스위치에 Manager, Rep 배치
values = ["Price"] --> 밸류위치에 Price 배치
columns = ["Product"] --> 컬럼위치에 Product배치
aggfunc = [np.sum, np.mean] --> np를 이용한 sum함수처리
fill_value = 0 --> NaN데이터를 0으로 처리