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')
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP