59doit
[Python-Numpy] -combining #2 본문
반응형
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], index=['c', 'd', 'e'])
s2
# <출력>
# c 2
# d 3
# e 4
# dtype: int64
s3 = pd.Series([5, 6], index=['f', 'g'])
s3
# <출력>
# f 5
# g 6
# dtype: int64
▷
concat()함수에 전달하면 값과 색인을 연결
pd.concat([s1, s2, s3])
# <출력>
# a 0
# b 1
# c 2
# d 3
# e 4
# f 5
# g 6
# dtype: int64
▷
# concat()함수는 axis=0 을 기본값으로 새로운 Series 객체를 생성
# 만약 axis=1 을 넘긴다면 결과는 DataFrame 이 된다(axis=1 은 컬럼을 의미)
pd.concat([s1, s2, s3], axis=1)
# <출력>
# 0 1 2
# a 0.0 NaN NaN
# b 1.0 NaN NaN
# c NaN 2.0 NaN
# d NaN 3.0 NaN
# e NaN 4.0 NaN
# f NaN NaN 5.0
# g NaN NaN 6.0
▷
join='inner'옵션을 사용하여 교집합을 구할 수도 있다
# <s1>
# a 0
# b 1
# <s3>
# f 5
# g 6
s4 = pd.concat([s1, s3])
s4
# <출력>
# a 0
# b 1
# f 5
# g 6
# dtype: int64
pd.concat([s1, s4], axis=1)
# <출력>
# 0 1
# a 0.0 0
# b 1.0 1
# f NaN 5
# g NaN 6
pd.concat([s1, s4], axis=1, join='inner')
# <출력>
# 0 1
# a 0 0
# b 1 1
▷
join_axes 인자로 병합하려는 축을 직접 지정
pd.concat([s1, s4], axis=1, join_axes=[['a', 'b', 'c', 'f']])
# join_axes : deprecated
# <출력>
# 0 1
# a 0.0 0.0
# b 1.0 1.0
# c NaN NaN
# f NaN 5.0
▷
계층적 색인을 생성하려면 keys 인자 사용
result = pd.concat([s1, s1, s3], keys=['one', 'two', 'three'])
result
# <출력>
# one a 0
# b 1
# two a 0
# b 1
# three f 5
# g 6
# dtype: int64
result.unstack()
# <출력>
# a b f g
# one 0.0 1.0 NaN NaN
# two 0.0 1.0 NaN NaN
# three NaN NaN 5.0 6.0
▷
axis=1 로 병합할 경우 keys 는 DataFrame 의 컬럼명이 된다.
pd.concat([s1, s2, s3], axis=1, keys=['one', 'two', 'three'])
# <출력>
# one two three
# a 0.0 NaN NaN
# b 1.0 NaN NaN
# c NaN 2.0 NaN
# d NaN 3.0 NaN
# e NaN 4.0 NaN
# f NaN NaN 5.0
# g NaN NaN 6.0
df1 = pd.DataFrame(np.arange(6).reshape(3, 2), index=['a', 'b', 'c'], columns=['one', 'two'])
df2 = pd.DataFrame(5 + np.arange(4).reshape(2, 2), index=['a', 'c'], columns=['three', 'four'])
df1
# <출력>
# one two
# a 0 1
# b 2 3
# c 4 5
df2
# <출력>
# three four
# a 5 6
# c 7 8
pd.concat([df1, df2], axis=1, keys=['level1', 'level2'])
# <출력>
# level1 level2
# one two three four
# a 0 1 5.0 6.0
# b 2 3 NaN NaN
# c 4 5 7.0 8.0
▷
리스트 대신 객체의 dict 를 이용하면 dict 의 키가 keys 옵션으로 사용된다.
pd.concat({'level1': df1, 'level2': df2}, axis=1)
# <출력>
# level1 level2
# one two three four
# a 0 1 5.0 6.0
# b 2 3 NaN NaN
# c 4 5 7.0 8.0
▷
새로 생성된 계층 names 로 지정
pd.concat([df1, df2], axis=1, keys=['level1', 'level2'], names=['upper', 'lower'])
# <출력>
# upper level1 level2
# lower one two three four
# a 0 1 5.0 6.0
# b 2 3 NaN NaN
# c 4 5 7.0 8.0
▷
DataFrame 의 로우색인이 분석에 필요한 데이터를 포함하고 있지 않은 경우
df1 = pd.DataFrame(np.random.randn(3, 4), columns=['a', 'b', 'c', 'd'])
df2 = pd.DataFrame(np.random.randn(2, 3), columns=['b', 'd', 'a'])
df1
# <출력>
# a b c d
# 0 1.246435 1.007189 -1.296221 0.274992
# 1 0.228913 1.352917 0.886429 -2.001637
# 2 -0.371843 1.669025 -0.438570 -0.539741
df2
# <출력>
# b d a
# 0 0.476985 3.248944 -1.021228
# 1 -0.577087 0.124121 0.302614
▷
이 경우 ignore_index=True 옵션 사용
pd.concat([df1, df2], ignore_index=True) # row bind
# <출력>
# a b c d
# 0 1.246435 1.007189 -1.296221 0.274992
# 1 0.228913 1.352917 0.886429 -2.001637
# 2 -0.371843 1.669025 -0.438570 -0.539741
# 3 -1.021228 0.476985 NaN 3.248944
# 4 0.302614 -0.577087 NaN 0.124121
반응형
'Programming > Python(파이썬)' 카테고리의 다른 글
[Pandas & Numpy] 시계열 데이터 csv (0) | 2022.11.12 |
---|---|
[Python-Numpy]-combining #3 (0) | 2022.11.11 |
[Python-Pandas] combining #1 (0) | 2022.11.11 |
[Python-Pandas] data index (0) | 2022.11.10 |
[Python-Pandas] 치환 (0) | 2022.11.10 |
Comments