728x90

파이썬(Python)/Pandas 10

Pandas code snippet

여러개의 csv 파일을 한개의 csv 파일로 만들기 import os import pandas as pd # 현재 디렉토리의 모든 파일 목록을 얻기 files = os.listdir('.') # 'result_숫자.csv' 형식에 맞는 파일만 필터링 csv_files = [file for file in files if file.startswith('result_') and file.endswith('.csv')] # 모든 CSV 파일을 합치기 combined_csv = pd.DataFrame() for file in csv_files: df = pd.read_csv(file) combined_csv = pd.concat([combined_csv, df]) # 결과 CSV 파일 저장 combined_csv..

pandas에서 describe() 사용하면 전체 숫자가 안나올 때

전체 숫자를 확인하고 싶지만 아래와 같이 나오는 경우가 있다. a b c 0 1.005544e+05 1.226455e+04 2.643200e+03 1 5.542502e+03 7.135909e+03 5.435605e+02 이 경우 아래와 같은 코드를 먼저 입력하면 전체 숫자를 볼 수 있다. pd.set_option('float_format', '{:.2f}'.format) a b c 0 10055 12264 2643 1 5542 7135 543 참고 stackoverflow.com/questions/41328633/how-do-i-print-entire-number-in-python-from-describe-function How do I print entire number in Python from de..

python에서 pandas dataframe에서 컬럼의 text가 모두 안보이는 경우

컬럼 값이 길경우 ....으로 줄여서 나오는 경우가 있다. 이 때는 아래와 같은 옵션을 데이터 조회 전에 실행하면 된다. 두 번째 파라미터의 -1은 노출할 텍스트 수의 제한이 없음을 의미한다. pd.set_option('display.max_colwidth', -1) 참고 stackoverflow.com/questions/25351968/how-to-display-full-non-truncated-dataframe-information-in-html-when-convertin How to display full (non-truncated) dataframe information in html when converting from pandas dataframe to html? I converted a pa..

Python의 DataFrame에서 모든 column이 나오게 하는 방법

판다스로 만든 컬럼이 많은 데이터프레임을 조회하면 모든 컬럼이 나오지 않는 경우가 있다. 이때는 아래와 같은 옵션을 데이터 조회 전에 실행하면 모든 컬럼을 볼 수 있다. 단 컬럼이 너무 많은 경우에는 에러가 발생한다. 두 번째 파라미터가 노출 컬럼 수이고, -1은 제한없이 보여준다는 의미다. pd.set_option('display.max_columns', -1) 참고 towardsdatascience.com/how-to-show-all-columns-rows-of-a-pandas-dataframe-c49d4507fcf

pandas dataframe에 row 추가하기

mem_no prd_no 0 100 1000 1 200 2000 위와 같은 df라는 이름의 DataFrame이 있을 때 한 행을 추가하고 싶을 수 있다. 이 때는 아래와 같이 진행하면 된다. import pandas as pd list1 = [300, 3000] df2 = df.append(pd.Series(list1, index=df.columns), ignore_index=True) df2 위와 같이 처리하면 한 행이 추가된 것을 확인할 수 있다. 참고 emilkwak.github.io/dataframe-list-row-append-ignore-index

pandas.DataFrame.iloc

DataFrame.iloc 정의 - 정수 값을 이용해 데이터프레임의 특정 위치를 인덱스 기반으로 선택한다. .iloc[]는 주로 0부터 축의 길이 -1 사이의 정수값으로 사용 가능 가능하지만, boolean array를 사용할 수도 있다. 아래와 같이 이용 가능하다 An integer, e.g. 5.A list or array of integers, e.g. [4, 3, 0].A slice object with ints, e.g. 1:7.A boolean array.A callable function with one argument (the calling Series, DataFrame or Panel) and that returns valid output for indexing (one of the a..

728x90