목록Programming/Python(파이썬) (44)
59doit
import numpy as np import pandas as pd pd.options.display.max_rows = 20 np.random.seed(12345) import matplotlib.pyplot as plt plt.rc('figure', figsize=(10, 6)) np.set_printoptions(precision=4, suppress=True) 시계열 데이터는 일반적으로 시간 순서대로 나열 data = pd.read_csv('C:/macrodata.csv') data.head() periods = pd.PeriodIndex(year=data.year, quarter=data.quarter, name='date') columns = pd.Index(['realgdp', 'infl'..
combining ▷ 벡터화된 if-else 구문을 표현하는 NumPy 의 where()함수 a = pd.Series([np.nan, 2.5, np.nan, 3.5, 4.5, np.nan], index=['f', 'e', 'd', 'c', 'b', 'a']) b = pd.Series(np.arange(len(a), dtype=np.float64), index=['f', 'e', 'd', 'c', 'b', 'a']) b[-1] = np.nan a # # f NaN # e 2.5 # d NaN # c 3.5 # b 4.5 # a NaN # dtype: float64 b # # f 0.0 # e 1.0 # d 2.0 # c 3.0 # b 4.0 # a NaN # dtype: float64 ▷ null 이면 b..
Numpy 는 ndarray 를 이어붙이는 concatenate()함수 제공 arr = np.arange(12).reshape((3, 4)) arr # # array([[ 0, 1, 2, 3], # [ 4, 5, 6, 7], # [ 8, 9, 10, 11]]) ▷ column 기준으로 병합 np.concatenate([arr, arr], axis=1) # # array([[ 0, 1, 2, 3, 0, 1, 2, 3], # [ 4, 5, 6, 7, 4, 5, 6, 7], # [ 8, 9, 10, 11, 8, 9, 10, 11]]) ▷ s1 = pd.Series([0, 1], index=['a', 'b']) s1 # # a 0 # b 1 # dtype: int64 s2 = pd.Series([2, 3, 4]..
combining df1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'], 'data1': range(7)}) df1 # # key data1 # 0 b 0 # 1 b 1 # 2 a 2 # 3 c 3 # 4 a 4 # 5 a 5 # 6 b 6 df2 = pd.DataFrame({'key': ['a', 'b', 'd'], 'data2': range(3)}) df2 # # key data2 # 0 a 0 # 1 b 1 # 2 d 2 ▷ merg() 함수 pd.merge(df1, df2) # # key data1 data2 # 0 b 0 1 # 1 b 1 1 # 2 b 6 1 # 3 a 2 0 # 4 a 4 0 # 5 a 5 0 ▷ merge()함수는 ..