59doit
[Pandas] arr 본문
반응형
▷
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
반응형
'Programming > Python(파이썬)' 카테고리의 다른 글
[Pandas] 넘파이에 의한 랜덤난수 (0) | 2022.11.08 |
---|---|
[Python] indexing (0) | 2022.11.08 |
[Pandas] #4 ser (0) | 2022.11.08 |
[Python-Pandas] #3 drop (0) | 2022.11.07 |
[Python-Pandas] #2 index (0) | 2022.11.07 |