언어 꿀Tip/Python 꿀tip! (67) 썸네일형 리스트형 07_01_38. pivot_table index를 column으로 (index to column) df_ff_01 = pd.pivot_table( df_ff , index =['part_datecd'] , columns='good_cls1cd', values=['ntsal_amt','sal_qty']) df_ff_01.columns = list(map('_'.join, df_ff_01.columns)) # index에 지정된 column이 index화 되기 때문에 그걸 다시 column으로 변경시킴 df_ff_01.reset_index(level=0, inplace=True) 07_01_35. local 환경에서 그래프(graph) 한글 깨짐 local PC 환경에서 그래프 생성시, 범례 혹은 title에서 한글 깨짐 발생 시 해결하고자 한다~ 아래 코드를 실행하면, 한글 폰트가 사용 가능해 짐 :) import platform import matplotlib if platform.system() == 'Windows': matplotlib.rc('font', family = 'Malgun Gothic') elif platform.system() == 'Darwin': matplotlib.rc('font', family = 'AppleGothic') else: matplotlib.rc('font', family = 'NanumGothic') matplotlib.rcParams['axes.unicode_minus'] = False 07_01_34. category 변수 다루기, handling category 다루는 방법 www.datacamp.com/community/tutorials/categorical-data 07_01_33. 한번에 lag 여러개 생성 l_col = ['sale_qty', 'sale_sku_cnt', 'ord_qty', 'ord_sku_cnt', 'new_sale_qty', 'new_sale_sku_cnt'] for col, lag in itertools.product(l_col, range(3, 29)): tmp_all[f'{col}_{str(lag)}d'] = tmp_all.groupby(['origin_bizpl_cd', 'ctgr_cd'])[col].shift(lag) 07_07_32. group별로 ts time series 한번에 만들기 # 일자 reindex datelist = pd.date_range( start = df.oper_dt.min(), end = df.oper_dt.max(), freq='D') df_01 = df.set_index('oper_dt').groupby(['origin_bizpl_cd', 'ctgr_cd']).apply(lambda x: x.reindex(datelist, fill_value=0)).drop(columns=['origin_bizpl_cd', 'ctgr_cd']).reset_index() # groupby reindex df_01 = df_01.rename(columns={'level_2':'oper_dt'}) 07_01_31. 데이터프레임 groupby 계산시 컬럼명(column) 변경 df_4w_ag = df_4w.groupby(["origin_bizpl_cd" , "goods_cd"] ).agg({"bg_purch_qty": [("sum_4w", "sum"), ("mean_4w", "mean"), ("max_4w", "max")], "bg_purch_qty_yn":[("count_4w", "sum")]}).droplevel(0, axis=1).reset_index() 07_01_30. convert multi index to single index (Series) # convert multi index to single index : reset_index s_roll_incdec = df_covid.groupby(["gubun"], group_keys=False)["incdec"].rolling(7).sum().reset_index(drop=True) 07_01_29. isocalendar() 일자의 주차 구하기 weeknum ## 특정 일자의 주차를 알고 싶을 때, ## isocalendar 가 튜플 타입으로 반환하며, 년, 주 datetime.date(2019,12,31).isocalendar()[0] datetime.date(2019,12,31).isocalendar()[1] 07_01_28. 오른쪽 특정 문자 삭제하기 ### 문자열 맨 오른쪽에 . 없애기 df_covid["qurrate"] = df_covid["qurrate"].str.rstrip('.') 왼쪽 특정 문자 삭제 lstrip('.') 양쪽 특정 문자 삭제 strip('.') 07_01_27. groupby rolling sum & Series name 변경 ## group 별로 rolling sum/average 하는 방법 ## rolling(n) : n 길이만큼 rolling 하겠다는 의미 즉, window size df_tmp[ df_tmp["oper_dt"].str[:6]=='201901'].groupby(["origin_bizpl_cd","ctgr_cd"], as_index=False)["buyget_sale_qty"].rolling(3).sum() # min_period=n : 데이터가 최소 n개라도 존재하면 값을 구하고 싶을 때 사용 df_tmp[ df_tmp["oper_dt"].str[:6]=='201901'].groupby(["origin_bizpl_cd","ctgr_cd"], as_index=False)["buyget_sale_qty"].r.. 이전 1 2 3 4 5 6 7 다음