본문 바로가기

Data science/데이터 기초,시각화

(8)
Pandas 기초(4) 시계열 다루기¶Time stamps는 특정 시점을 말한다.(예: 2024년 8월 12일 오전2시)Time intervals and periods는 특정 시작점과 종료점 사이의 길이를 말한다.Time deltas or durations는 정확한 시간 길이를 말한다(예: 22.6초)In [7]:import numpy as npimport pandas as pdfrom datetime import datetimefrom dateutil import parserIn [6]:# datetime 타입을 사용해 날짜를 직접 구성할 수 있다.print(datetime(year = 2024, month=8, day=12))# dateutil 모듈을 이용해 다양한 문자열 형태로부터 날짜를 해석할 수 있다.date = pa..
Pandas 기초(3) In [109]:import numpy as npimport pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltimport matplotlib as mplfrom pandas.core.reshape.reshape import unstack집계, 필터, 변환, 적용¶In [2]:rng = np.random.RandomState(0)df = pd.DataFrame({'key' : ['A', 'B', 'C', 'A', 'B', 'C'], 'data1' : range(6), 'data2' : rng.randint(0, 10, 6)}, columns = [..
Pandas 기초(2) In [2]:import pandas as pdimport numpy as npfrom bokeh.colors.named import greenyellow데이터세트 결합하기: 병합과 조인¶In [11]:df1 = pd.DataFrame({'name' : ['kim', 'han', 'min'], 'group' : ['badminton', 'us', 'us'] })df2 = pd.DataFrame({'name' : ['kim', 'han', 'min'], 'height' : ['192', '176', '160'] })print(df1);print(df2, '\n')df3 =..
Pandas 기초(1) In [3]:import numpy as npimport pandas as pdfrom numpy.array_api import truncPandas Series 객체¶In [4]:# Series는 인덱싱된 데이터의 1차원 배열이다data = pd.Series([0.25, 0.5, 0.75, 1.0])dataOut[4]:0 0.251 0.502 0.753 1.00dtype: float64In [5]:# values는 NumPy 배열이다.print(data.values)[0.25 0.5 0.75 1. ]Series: 일반화된 NumPy 배열¶In [9]:# Series는 인덱스로 문자열을 사용할 수 있다data = pd.Series([0.25, 0.5, 0.75, 1.0], ind..
NumPy 기초(3) 예제 미국 대통령이 평균 신장은 얼마일까?In [2]:from matplotlib.lines import drawStylesfrom sympy import seriesfrom sympy.physics.units import inches!head -4 ./president_heights.csvorder,name,height(cm)1,George Washington,1892,John Adams,1703,Thomas Jefferson,189In [3]:import pandas as pdimport numpy as npIn [8]:data = pd.read_csv('./president_heights.csv')heights = np.array(data['height(cm)'])dataOut[8]:ordernam..
NumPy 기초(2) 배열 연산: 유니버설 함수In [54]:import numpy as npfrom scipy import special 작은 연산 반복은 느리다 역수가 계산될 때마다 파이썬은 먼저 객체의 타입을 확인하고 해당 타입에 맞게 사용할 적절한 함수를 동적으로 검색한다In [18]:np.random.seed(0)def compute_reciprocals(val): output = np.empty(len(val)) for i in range(len(val)): output[i] = 1.0 / val[i] return outputval = np.random.randint(1, 10, size = 5)%timeit compute_reciprocals(val)big_val = np.rando..
NumPy 기초(1) 파이썬 리스트에서 배열 만들기In [3]:import numpy as npfrom mpmath import linspacefrom numpy.core.numeric import newaxisIn [9]:np.array([1,2,3,4,5])Out[9]:array([1, 2, 3, 4, 5])NumPy는 배열의 모든 요소가 같아야한다아니라면 상위 타입을 취하게 된다.In [12]:np.array([1.2,3,4,5,])Out[12]:array([1., 2., 3., 4.])In [13]:np.array([1,2,3,4], dtype=float) # 데이터 타입 설정Out[13]:array([1., 2., 3., 4.])In [15]:np.array([range(i, i + 3) for i in [2,..
Jupyter Notebook 기본 주요 단축기 m 마크다운 모드 y 코드 모드 command + enter 셀 실행 dd 셀 삭제 a 셀 위에 신규 셀 추가 b 셀 아래에 신규 셀 추가 command + / 주석추가 마크다운 모드