Python Pandas - 用索引对象中的指定值填充 NaN 值
要使用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])
显示熊猫指数-
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
#还使用一些NaN值创建Pandas索引
index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30])
#显示Pandas索引
print("Pandas Index...\n",index)
#返回索引中的元素数
print("\nNumber of elements in the index...\n",index.size)
#返回数据的dtype
print("\nThe dtype object...\n",index.dtype)
#用一些特定的值填充NaN
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')