Python - 在 Pandas DataFrame 中顯示無窮值對應的 True


使用 isin() 方法來顯示無窮值對應的 True。首先,我們用它們各自的別名匯入所需的庫 -

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)

顯示無窮值對應的 True -

res = dataFrame.isin([np.inf, -np.inf])

示例

以下是程式碼 -

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

# Displaying TRUE for infinite values
res = dataFrame.isin([np.inf, -np.inf])
print"\n Updated DataFrame...\n",res

輸出

這將產生以下輸出 -

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

Updated DataFrame...
   Reg_Price
0      False
1       True
2      False
3       True
4      False
5      False
6      False
7       True

更新於: 2021 年 9 月 20 日

230 次瀏覽

開啟你職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.