본문 바로가기
728x90
반응형

python29

파이썬(Python) Pandas Dataframe Dataframe Dataframe은 파이썬 Pandas의 자료 구조 중 하나이다. 다수의 Series를 하나의 Dataframe에서 관리할 수 있다. 코드 d1={'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6} d2={'a':7, 'b':8, 'c':9, 'd':10, 'e':11, 'f':12} s1 = pd.Series(d1) s2 = pd.Series(d2) df = pd.DataFrame({'d1': d1, 'd2':d2}) print(df) 출력 d1 d2 a 1 7 b.. 2022. 8. 3.
파이썬(Python) Pandas Series Series Series는 파이썬 Pandas의 자료 구조 중 하나이다. 코드 import pandas as pd import numpy as np pd.__version__ #Pandas version 확인 s = pd.Series([0, 1, 2, 3, 4, 5]) print(s) 현재 사용중인 Pandas 의 버전은 version 명령으로 확인 가능하다. Series를 만든 후 print 명령을 통해 내용을 출력할 수 있다. 출력 0 0 1 1 2 2 3 3 4 4 5 5 dtype: int64 만약 인덱스를 직접 지정하고 싶은 경우 아래 처럼 index를 지정할 수 있다. 코드 import pandas as pd import numpy as np pd.__version__ #Pandas versi.. 2022. 8. 3.
파이썬(Python) 문자열 포멧(String format) String format 문자열의 서식을 지정한다. %를 이용하는 방법, format 함수, 그리고 f-string을 이용하는 방법이 있다. 가장 편하고 추천하는 방법이 f-string이다. 목차 문자열 출력 문자열 출력 코드 num = 10 str = 'python' print('number: %d, string: %s' % (num, str)) print('number: {}, string: {}'.format(num, str)) print(f'number: {num}, string: {str}') 출력 number: 10, string: python number: 10, string: python number: 10, string: python 2022. 2. 19.
파이썬(Python) 패키지 설치 및 삭제 패키지 설치 및 삭제 패키지 설치 pip install 패키지이름패키지 삭제 pip uninstall 패키지이름설치된 패키지 확인 pip list 2022. 2. 12.
Google Colab 패키지(Package) 영구적 설치 Google Colab 패키지(Package) 영구 설치 매번 실행할 때마다 패키지를 새로 설치하는게 너무 번거롭고 시간 소비가 심한것 같아서 영구적 설치를 적용했습니다. 아래 명령을 실행하면 /content/packages 심볼릭 링크를 생성하고 path 환경 변수에 경로를 추가합니다. 간단하게 말하면 /content/drive 에 패키지를 설치하면 실제 설치 경로는 /content/drive/My Drive/Colab Notebooks 에 설치됩니다. import os, sys from google.colab import drive drive.mount('/content/drive') pg_path = '/content/packages' os.symlink('/co.. 2021. 9. 25.
파이썬(Python) 공백 제거 및 빈 문자열 확인 공백 제거 및 빈 문자열 확인 목차 strip strip 문자열 양쪽의 공백 문자를 제거한다. 코드 # 공백 제거 string = ' Python '.strip() print(string) # 빈 문자열 확인 string = ' ' if not string.strip(): print("문자 없음") else: print("문자 있음") 출력 Python 문자 없음 2021. 9. 25.
파이썬(Python) collections Counter collections Counter 리스트의 항목 갯수를 셀수 있습니다. 목차 Counter update most_common Counter 항목과 항목 수를 딕셔너리 형태로 리턴 받을 수 있습니다. most_common 가장 많은 빈도를 가진 항목 순으로(내림 차순) 정렬된 딕셔너리 데이터를 받을 수 있습니다. update 기존 항목에 다른 항목을 추가할 수 있습니다. 코드 from collections import Counter list1 = ['a', 'a', 'b', 'c', 'd', 'c'] list2 = ['b', 'b', 'b'] count = Counter(list1) print('result #1: ', count) count.update(list2) print('result #2: ',.. 2021. 9. 22.
파이썬(Python) Numpy 행렬 연결 배열, 행렬 연결하기 목차 concatenate vstack, hstack stack concatenate 두 행렬을 지정된 축에 따라 연결할 수 있습니다. 2차원 배열의 경우 행으로 연결하기 위해서는 axis = 0, 열은 axis = 1 을 지정하면 됩니다. 코드 import numpy as np arr1 = np.array([[1, 2, 3], [4, 5, 6]]) arr2 = np.array([[7, 8, 9], [10, 11, 12]]) print( np.concatenate((arr1, arr2), axis = 0) ) 출력 array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [10, 11, 12]]) 코드 import numpy as np arr1 = np.array.. 2021. 9. 4.
파이썬(Python) Numpy 딕셔너리(Dictionary) 딕셔너리(Dictionary) 목차 딕셔너리 선언 딕셔너리 삭제 딕셔너리 활용 방법 딕셔너리 선언 키와 값의 쌍으로 데이터를 처리할 수 있으며 키 값은 중복이 불가능합니다. 다양한 선언 방법이 존재합니다. 코드 dic = dict() dic['key1'] = 1 dic['key2'] = 2 print(dic) dic = dict(key1 = 1, key2 = 2) print(dic) dic = {'key1':1, 'key2':2} print(dic) 출력 {'key1': 1, 'key2': 2} {'key1': 1, 'key2': 2} {'key1': 1, 'key2': 2} 딕셔너리 삭제 del 명령으로 항목을 삭제할 수 있습니다. 코드 dic = {'key1':1, 'key2':2} print(dic.. 2021. 9. 4.
728x90
반응형