yul_S2 2022. 11. 8. 09:07
반응형

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     d     a
# Utah    0.0   1.0   2.0
# Ohio    3.0   4.0   5.0
# Texas   6.0   7.0   8.0
# Oregon  9.0  10.0  11.0

series
# <출력>
# b    0.0
# d    1.0
# a    2.0
# Name: Utah, dtype: float64

frame series 산술연산

frame-series 
# <출력>
#           b    d    a
# Utah    0.0  0.0  0.0
# Ohio    3.0  3.0  3.0
# Texas   6.0  6.0  6.0
# Oregon  9.0  9.0  9.0

 컬럼 맞추고 로우로 전파

 

 

 

객체는 형식을 맞추기 위해 재색인된다.

series2 = pd.Series(range(3), index=['b', 'e', 'f'])
series2
# <출력>
# b    0
# e    1
# f    2
# dtype: int64

 

 

frame series 산술연산

# <frame>
#           b     d     a
# Utah    0.0   1.0   2.0
# Ohio    3.0   4.0   5.0
# Texas   6.0   7.0   8.0
# Oregon  9.0  10.0  11.0
frame + series2
# <출력>
#          a    b   d   e   f
# Utah   NaN  0.0 NaN NaN NaN
# Ohio   NaN  3.0 NaN NaN NaN
# Texas  NaN  6.0 NaN NaN NaN
# Oregon NaN  9.0 NaN NaN NaN

 

 

series3 = frame['d']
frame
# <출력>
#           b     d     a
# Utah    0.0   1.0   2.0
# Ohio    3.0   4.0   5.0
# Texas   6.0   7.0   8.0
# Oregon  9.0  10.0  11.0

series3
# <출력>
# Utah       1.0
# Ohio       4.0
# Texas      7.0
# Oregon    10.0
# Name: d, dtype: float64

 

 

 

.sub

frame.sub(series3, axis = 'index')
# <출력>
#           b    d    a
# Utah   -1.0  0.0  1.0
# Ohio   -1.0  0.0  1.0
# Texas  -1.0  0.0  1.0
# Oregon -1.0  0.0  1.0

 

 

반응형