목록Programming (97)
59doit
import pandas as pd import numpy as np ▷ 넘파이에 의한 랜덤난수 frame = pd.DataFrame(np.random.randn(4, 3), columns=list('bde'), index=['Utah', 'Ohio', 'Texas', 'Oregon']) frame # # b d e # Utah 0.274992 0.228913 1.352917 # Ohio 0.886429 -2.001637 -0.371843 # Texas 1.669025 -0.438570 -0.539741 # Oregon 0.476985 3.248944 -1.021228 np.abs(frame) ▷ lambda f = lambda x:x.max() - x.min() frame.apply(f) # # b 1..
numpy 는 정수여야만 했지만 판다스는 정수외에 실수도 사용가능 ▷ obj = pd.Series(np.arange(4.), index=['a', 'b', 'c', 'd']) obj # # a 0.0 # b 1.0 # c 2.0 # d 3.0 # dtype: float64 obj['b'] # 1.0 obj[1] # 1.0 obj[2:4] # # c 2.0 # d 3.0 # dtype: float64 obj[['b', 'a', 'd']] # # b 1.0 # a 0.0 # d 3.0 # dtype: float64 obj[[1, 3]] # # b 1.0 # d 3.0 # dtype: float64 obj[obj < 2] # # a 0.0 # b 1.0 # dtype: float64 obj['b':'c'] #..
▷ arr = np.arange(12.).reshape((3,4)) arr # # array([[ 0., 1., 2., 3.], # [ 4., 5., 6., 7.], # [ 8., 9., 10., 11.]]) ▷ arr[0] # # array([0., 1., 2., 3.]) arr-arr[0] # # array([[0., 0., 0., 0.], # [4., 4., 4., 4.], # [8., 8., 8., 8.]]) 각 로우별 한번씩 수행 ▷ frame = pd.DataFrame(np.arange(12.).reshape((4,3)), columns=list('bda'), index=['Utah','Ohio','Texas','Oregon']) series = frame.iloc[0] frame # # b ..
ser ser = pd.Series(np.arange(3.)) ser # # 0 0.0 # 1 1.0 # 2 2.0 # dtype: float64 ▷ 정수 기반의 색인을 사용하지 않는 경우 이런 모호함은 사라진다. ser2 = pd.Series(np.arange(3.), index=['a', 'b', 'c']) ser2[-1] # 2.0 ▷ 일관성 유지를 위해 정수값을 담고 있는 축 색인이 있다면 우선적으로 라벨을 먼저 찾아보도록 구현되어 있다. ser[:1] # # 0 0.0 # dtype: float64 ▷ 라벨 컬럼 => loc 을 사용 ser.loc[:1] # # 0 0.0 # 1 1.0 # dtype: float64 ▷ 정수 색인 row =>iloc 을 사용 ser.iloc[:1] # # 0 0..