Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果同一元素在数据帧中出现两次?_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 如果同一元素在数据帧中出现两次?

Python 如果同一元素在数据帧中出现两次?,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,数据框中的第一列是StudentID的随机列表。我想知道是否有学生ID出现过两次。如果是这样的话,我想把它发生的两行打印出来 StudentID Name s123456 Michael s123789 Peter s123789 Thomas s123579 Marie 我想打印出: "Two students have the same student id in line {} and {}" 这里有一种使用f字符串的方法,在Python 3.6+

数据框中的第一列是StudentID的随机列表。我想知道是否有学生ID出现过两次。如果是这样的话,我想把它发生的两行打印出来

StudentID   Name
s123456     Michael
s123789     Peter
s123789     Thomas 
s123579     Marie
我想打印出:

"Two students have the same student id in line {} and {}"

这里有一种使用f字符串的方法,在Python 3.6+中提供:

# example data
StudentID   Name
s123456     Michael
s123789     Peter
s123789     Thomas 
s123577     Joe
s123456     Mark
s123458     Andrew

# get duplicates StudentIDs
dups = df.loc[df['StudentID'].duplicated(keep=False), 'StudentID'].unique()

# iterate duplicates
for stid in dups:
    dup_idx = df[df['StudentID'] == stid].index.tolist()
    print(f'{len(dup_idx)} Students have the same student id in lines: {dup_idx}')

2 Students have the same student id in lines: [0, 4]
2 Students have the same student id in lines: [1, 2]

到目前为止你试过什么?可能重复,但我想打印出原始数据框中的哪一行,这两个studentId。@LauraRøtzlerHolm您的意思是您也想打印
s123789
作为句子的一部分吗?不,我想打印出这两个studentId的行号。比如,第1行和第2行。@LauraRøtzlerHolm我已经相应地更新了答案
# example data
StudentID   Name
s123456     Michael
s123789     Peter
s123789     Thomas 
s123577     Joe
s123456     Mark
s123458     Andrew

# get duplicates StudentIDs
dups = df.loc[df['StudentID'].duplicated(keep=False), 'StudentID'].unique()

# iterate duplicates
for stid in dups:
    dup_idx = df[df['StudentID'] == stid].index.tolist()
    print(f'{len(dup_idx)} Students have the same student id in lines: {dup_idx}')

2 Students have the same student id in lines: [0, 4]
2 Students have the same student id in lines: [1, 2]