본문 바로가기

프로그래밍/Python

(5)
[python] 막대그래프 (bar chart) 위에 수치 표시하기 x = dfcb['존속년수구분'].tolist() y = dfcb['사업장명'].tolist() plt.figure(figsize=(8,6)) #그래프 크기 bar = plt.bar(x,y) #그래프 수치 표현 for rect in bar: height = rect.get_height() plt.text(rect.get_x()+rect.get_width()/2.0,height, height, ha = 'center',va='bottom',size=12) plt.xticks(rotation=45) # x축 45도 회전 plt.title('존속년수 비교') plt.xlabel('존속년수') plt.ylabel('가게수') plt.show() x , y 값엔 리스트 형태의 값이 나와야 한다. 그래서 시리즈 형..
[python] 특정 날짜가 생략되지 않게 하기 월별 데이터를 그룹화하면 특정 월에서는 1이상의 숫자가 나오지 않아 자동으로 생략된다. 그래프에서 0으로 나타난 월도 보고싶을 때 사용하는 방법이다. list_df_g = ['2017-01', '2017-02','2017-03','2017-04','2017-05','2017-06','2017-07','2017-08','2017-09','2017-10','2017-11','2017-12', '2018-01', '2018-02', '2018-03', '2018-04', '2018-05', '2018-06', '2018-07', '2018-08', '2018-09', '2018-10', '2018-11', '2018-12', '2019-01','2019-02','2019-03','2019-04','2019-0..
python endswith list 특정 문자 여러개로 끝나는 행 추출 여러 개의 단어로 끝나는 행을 한번에 보고 싶을 떄, endswith를 사용하고 싶을 때 # 포함하고자 하는 문자열 리스트 생성 example_list = ['슈퍼','수퍼','마트','마켓'] result = df[df['사업장명'].str.endswith(tuple(example_list))] result 사업장명 열에서 슈퍼,수퍼,마트,마켓으로 끝나는 행을 보고 싶었다. (OR)조건으로 여기서 핵심은 리스트형인 example_list를 튜플로 변환하여 endswith에 넣는 것이다. contains을 사용시 기업이름 중간에 스마트가 들어간 경우가 있었다. -> endswith 사용 참고 https://howtodoinjava.com/python/functions/string-functions/str..
파이썬으로 여러 엑셀 파일 필터링 후 한 파일로 합치기 한 폴더에 포함된 여러 데이터 파일을 한 번에 필터링하고 파일 하나로 합치고 싶을 때 사용하는 자동화 코드입니다. import os import pandas as pd path = 'C:/상권분석/유동인구/' #파일 경로명 변경 file_list = os.listdir(path) file_list_py = [file for file in file_list if file.endswith('.csv')] excel = pd.DataFrame() for i in file_list_py: df = pd.read_csv(path+i,encoding='euc-kr') df = df[df['행정동'] =='충장동'] #필터링 excel = excel.append(df,ignore_index=True) #파일 하나에 ..
파이썬 if __name__ == "__main__" 인터프리터에서 직접 실행했을 경우에만 if문 내의 코드를 돌리라는 명령 #!/usr/bin/python # Filename: using_name.py if __name__ == '__main__': print 'This program is being run by itself' else: print 'I am being imported from another module' $ python using_name.py #인터프리터에서 직접 실행 This program is being run by itself $ python >>> import using_name #임포트(import)해서 실행 I am being imported from another module ..