Python - 仅显示来自 DataFrame 的非重复值
我们将看到如何仅显示非重复值。首先,我们将创建一个具有重复值的DataFrame-
dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'],"Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'] } )
上面,我们创建了2列。要仅显示非重复值,请使用duplicated()方法和逻辑NOT。通过这种方式,将获取非重复值-
dataFrame[~dataFrame.duplicated('Student')]
示例
以下是完整的代码-
import pandas as pd #创建数据帧 dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'],"Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'] } ) print"DataFrame ...\n",dataFrame #显示非重复 res = dataFrame[~dataFrame.duplicated('Student')] print"\nDataFrame after removing duplicates ...\n",res输出结果
这将产生以下输出-
DataFrame ... Result Student 0 Pass Jack 1 Fail Robin 2 Pass Ted 3 Fail Robin 4 Pass Scarlett 5 Pass Kat 6 Pass Ted DataFrame after removing duplicates ... Result Student 0 Pass Jack 1 Fail Robin 2 Pass Ted 4 Pass Scarlett 5 Pass Kat