NaN을 마스킹하고 특정 값으로 바꾸려면 index.putmask()를 사용하세요. 방법. 그 안에서 index.isna() 메서드를 설정합니다.
먼저 필요한 라이브러리를 가져옵니다 -
import pandas as pd import numpy as np
일부 NaN을 사용하여 Pandas 인덱스 만들기 −
index = pd.Index([5, 65, 10, np.nan, 75, np.nan])
판다 인덱스 표시 -
print("Pandas Index...\n",index) NaN 인덱스 값을 마스크하고 특정 값으로 대체 -
print("\nMask...\n",index.putmask(index.isna(), 111))
예시
다음은 코드입니다 -
import pandas as pd
import numpy as np
# Creating Pandas index with some NaNs
index = pd.Index([5, 65, 10, np.nan, 75, np.nan])
# Display the Pandas index
print("Pandas Index...\n",index)
# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)
# mask and replace NaN index values with a specific value
print("\nMask...\n",index.putmask(index.isna(), 111)) 출력
이것은 다음과 같은 출력을 생성합니다 -
Pandas Index... Float64Index([5.0, 65.0, 10.0, nan, 75.0, nan], dtype='float64') Number of elements in the index... 6 Mask... Float64Index([5.0, 65.0, 10.0, 111.0, 75.0, 111.0], dtype='float64')