Python Pandas - 返回无 NaN 值的索引
要返回没有 NaN 值的索引,请在 Pandas 中使用 index.dropna() 方法。首先,导入所需的库 -
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("\nThe Index object after removing NaN values...\n",index.dropna())示例
代码如下 -
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)
# Drop only the NaN values
print("\nThe Index object after removing NaN values...\n",index.dropna())输出
这将产生以下输出 -
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 The Index object after removing NaN values... Float64Index([50.0, 10.0, 70.0, 90.0, 50.0, 30.0], dtype='float64')
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP