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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP