파이썬완전정복-Pandas DataFrame/08. 결합
Pandas DataFrame 08-02. 다른 객체로 결측치 덮어쓰기 (combine_first)
알고리즘 트레이딩을 위한 파이썬
2022. 1. 23. 00:00
반응형
08-02. 다른 객체로 결측치 덮어쓰기 (combine_first)
DataFrame.combine_first(other)
개요
combine_first 메서드는 other의 값으로 self(df)의 NaN값을 덮어쓰는 메서드입니다.
사용법
self.combine_first(other)
other : self객체의 결측치를 덮어쓸 객체 입니다.
예시
기본 사용법
먼저 np.NaN이 포함된 간단한 3x3짜리 데이터 2개를 만들어보겠습니다.
n=np.NaN
col = ['col1','col2','col3']
row = ['row1','row2','row3']
data1 = [[n,n,1],
[n,n,1],
[1,1,1]]
data2 = [[2,2,2],
[2,n,2],
[2,1,2]]
df1 = pd.DataFrame(data1,row,col)
df2 = pd.DataFrame(data2,row,col)
print(df1)
>>
col1 col2 col3
row1 NaN NaN 1
row2 NaN NaN 1
row3 1.0 1.0 1
print(df2)
>>
col1 col2 col3
row1 2 2.0 2
row2 2 NaN 2
row3 2 1.0 2
기본적인 사용법
self객체의 NaN값을 other객체의 같은위치의 인수로 덮어쓰기 합니다.
만약 self에서 NaN인 값이 other에서도 NaN이라면 NaN을 출력합니다.
print(df1.combine_first(df2))
>>
col1 col2 col3
row1 2.0 2.0 1
row2 2.0 NaN 1
row3 1.0 1.0 1
반응형