Python Pandas - 用 infinity 檢查和顯示行索引


使用 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

# dictionary of list
d = { "Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf] }

# creating dataframe from the above dictionary of list
dataFrame = pd.DataFrame(d)
print"DataFrame...\n",dataFrame

# checking for infinite values and displaying the count
count = np.isinf(dataFrame).values.sum()
print"\nInfinity values count...\n ",count

# getting row index with infinity values
indexNum = dataFrame.index[np.isinf(dataFrame).any(1)]
print"\nDisplay row indexes with infinite values...\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

Infinity values count...
   3

Display row indexes with infinite values...
   Int64Index([1, 3, 7], dtype='int64')

更新日期:2021-09-20

超過 1K 的瀏覽量

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告