Python – 只显示 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 # Create DataFrame dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'],"Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'] } ) print"DataFrame ...\n",dataFrame # displaying non-duplicates 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
广告