Python Pandas – 检查并显示无穷大的行索引
pythonserver side programmingprogramming更新于 2025/11/8 23:08:17
要检查并显示行索引,请使用 isinf() 和 any()。首先,让我们导入所需的库及其各自的别名 −
import pandas as pd import numpy as np
创建一个列表字典。我们已经使用 Numpy np.inf 设置了无穷大值 −
d = { "Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf] }
从上面的列表字典创建 DataFrame −
dataFrame = pd.DataFrame(d)
获取具有无穷大值的行索引 −
indexNum = dataFrame.index[np.isinf(dataFrame).any(1)]
示例
以下是代码 −
import pandas as pd
import numpy as np
# 列表字典
d = { "Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf] }
# 从上面的列表字典创建数据框
dataFrame = pd.DataFrame(d)
print"DataFrame...\n",dataFrame
# 检查无限值并显示计数
count = np.isinf(dataFrame).values.sum()
print"\n无穷大值计数...\n ",count
# 获取具有无穷大值的行索引
indexNum = dataFrame.index[np.isinf(dataFrame).any(1)]
print"\n显示具有无穷大值的行索引...\n ",indexNum
输出
这将产生以下输出 −
DataFrame... Reg_Price 0 7000.505700 1 inf 2 5000.000000 3 inf 4 9000.757680 5 6000.000000 6 900.000000 7 inf 无穷大值计数... 3 显示具有无穷大值的行索引... Int64Index([1, 3, 7], dtype='int64')
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

