Python - 檢查 Pandas 資料幀是否包含無窮大值


若要檢查,請使用 isinf() 方法。若要查詢無窮大值的數量,請使用 sum()。首先,讓我們使用各自的別名匯入所需的庫 −

import pandas as pd
import numpy as np

建立列表字典。我們使用 Numpy np.inf 設定無窮大值 −

d = { "Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000] }

從上述列表字典建立資料幀

dataFrame = pd.DataFrame(d)

使用 isinf() 檢查無窮大值並顯示數量

count = np.isinf(dataFrame).values.sum()

示例

以下為程式碼 −

import pandas as pd
import numpy as np

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

# 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...\n ",count

輸出

將產生以下輸出 −

DataFrame...
     Reg_Price
0  7000.505700
1          inf
2  5000.000000
3          inf
4  9000.757680
5  6000.000000

Infinity values...
   2

更新於: 2021 年 9 月 22 日

7K+ 瀏覽量

開啟你的 職業生涯

完成課程,獲得認證

開始學習
廣告