Python Pandas - 使用指定的 value 填充 Index 物件中的 NaN 值
若要使用指定的 value 填充 Index 物件中的 NaN 值,請在 Pandas 中使用 index.fillna() 方法。首先,匯入所需的庫-。
import pandas as pd import numpy as np
建立具有 NaN 值的 Pandas 索引-。
index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30])
顯示 Pandas 索引 -
print("Pandas Index...\n",index)使用特定值填充 NaN 值 -
print("\nIndex object after filling NaN value...\n",index.fillna('Amit'))
示例
以下為程式碼 -
import pandas as pd
import numpy as np
# Creating Pandas index with some NaN values as well
index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30])
# 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)
# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)
# Fill the NaN with some specific value
print("\nIndex object after filling NaN value...\n",index.fillna('Amit'))輸出
這將生成以下輸出 -
Pandas Index... Float64Index([50.0, 10.0, 70.0, nan, 90.0, 50.0, nan, nan, 30.0], dtype='float64') Number of elements in the index... 9 The dtype object... float64 Index object after filling NaN value... Index([50.0, 10.0, 70.0, 'Amit', 90.0, 50.0, 'Amit', 'Amit', 30.0], dtype='object')
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP