Python Pandas - 显示每列中存在的唯一值
要在每列中显示唯一值,请使用该unique()方法并在其中设置列。首先,导入所需的库 -
import pandas as pd
创建一个包含两列和重复记录的 DataFrame -
dataFrame = pd.DataFrame({
"Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'],"Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass']
}
)
通过在unique()方法中设置每一列来查找唯一值-
resStudent = pd.unique(dataFrame.Student)resResult = pd.unique(dataFrame.Result)
示例
以下是代码 -
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
# 寻找独特的价值
resStudent = pd.unique(dataFrame.Student)
resResult = pd.unique(dataFrame.Result)
print"\nUnique values in Student Column", resStudent
print"\nUnique values in Result Column", resResult
这将产生以下输出 -
DataFrame ...Result Student
0 Pass Jack
1 Fail Robin
2 Pass Ted
3 Fail Robin
4 Pass Scarlett
5 Pass Kat
6 Pass Ted
Unique values in Student Column ['Jack' 'Robin' 'Ted' 'Scarlett' 'Kat']
Unique values in Result Column ['Pass' 'Fail']
以上是 Python Pandas - 显示每列中存在的唯一值 的全部内容, 来源链接: utcz.com/z/347531.html