본문 바로가기

언어 꿀Tip/Python 꿀tip!

(67)
07_01_77. 그룹별 describe() 구하기 : groupby describe() # Making GroupBy object grouped = df.groupby('cd') ## 1) grouped.describe()['cnt'] ## 2) T : transpose grouped.describe()['cnt'].T 2) output 예시
07_01_76. period_range 날짜생성 후 년월 string 변환 pd.period_range('20180101', '20210801', freq='M').astype(str).str.replace("-","")
07_01_74. dataframe difference of dates df_agg = df_agg.sort_values(['cust_distn_no','oper_dt']).reset_index(drop=True) # column type 변경 df_agg['oper_date'] = pd.to_datetime(df_agg['oper_dt']) # lag변수 생성 df_agg['lag_oper_date'] = df_agg.groupby(['cust_distn_no'])['oper_date'].shift(1) df_agg_01 = df_agg[~df_agg['lag_oper_date'].isnull()] # difference df_agg_01['diff_oper_date'] = (df_agg_01['oper_date']-df_agg_01['lag_oper_date']).dt...
07_02_02. [codility] CyclicRotation python import numpy as np 가 실행되지 않아, 결국 다른 분들이 작성한 것을 참고하여 풀어봄! Task description An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the f..
07_02_01. [codility] OddOccurrencesInArray collection 모듈 Counter 이용 문제 ! Task description A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired. For example, in array A such that: A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9 the elements at indexes 0 and 2 have valu..
07_01_71. sql로 데이터프레임(dataframe) 다루기, pysqldf sql을 활용하여 데이터프레임 join 등등 하기 위한 기본 구문 from pysqldf import SQLDF import pandas as pd import io txt = io.StringIO(""" col1col2 a1 b2 c3 d4 """) df = pd.read_csv(txt, sep="\t") df sqldf = SQLDF(globals()) sqldf.execute("select * from df limit 3;")
07_01_70. 두 list 중복값 추출하기 두개 데이터 프레임을 merge하려고 함 이 때, 컬럼이 너무 많아서 모두 key로 둘수도 없기 때문에 중복 컬럼들이 무엇인지 확인하고자 함 그래서 컬럼명들을 list화 해서 중복값 추출하고자 함 ## 두개 컬럼 명 list를 가지고 중복인 것 추출하고자 함 col_pv1 = df_pv1.columns.tolist() col_pv2 = df_pv2.columns.tolist() col_dup_pv = list(set(col_pv1).intersection(col_pv2))
07_01_69. list 내 값 중복 제거 # Python 3 code to demonstrate # removing duplicated from list # using collections.OrderedDict.fromkeys() from collections import OrderedDict # using collections.OrderedDict.fromkeys() # to remove duplicated # from list res = list(OrderedDict.fromkeys(l_features)) res
07_01_68. pd.pivot_table 컬럼명 prefix 컬럼명이 cd_cd의 value값 결합이 되도록 prefix 지정~ pd.pivot_table( df_goods , index = 'key', columns ='cd', values='qty').add_prefix('cd_')
07_01_67. 더미변수 생성하기 get_dummies import pandas as pd # dummy variable 생성 # drop_first = True : 첫번째 컬럼 base column으로 지정 pd.get_dummies(df_01['cd'] , prefix ='cd', drop_first=True)