在循环数据框

时间:2016-12-21 08:01:07

标签: python pandas

我有一个数据帧,每行重复3次。循环浏览它时,我如何确定我之前是否已经看过一行,然后做一些事情,即在循环中第二次出现时打印一些东西?

print df
       user     date
0      User001  2014-11-01
40     User001  2014-11-01
80     User001  2014-11-01
120    User001  2014-11-08
200    User001  2014-11-08
160    User001  2014-11-08
280    User001  2014-11-15
240    User001  2014-11-15
320    User001  2014-11-15
400    User001  2014-11-22
440    User001  2014-11-22
360    User001  2014-11-22
...    ......   ..........
...    ......   ..........
1300   User008  2014-11-22
1341   User008  2014-11-22
1360   User008  2014-11-22

for line in df.itertuples():
    user = line[1]
    date = line[2]

    print user, date
    #do something after second occurrence of tuple i.e. print "second occurrence"

('User001', '2014-11-01')
('User001',  '2014-11-01')
second occurrence
('User001',  '2014-11-01')
('User001',  '2014-11-08')
('User001',  '2014-11-08')
second occurrence
('User001',  '2014-11-08')
('User001',  '2014-11-15')
('User001',  '2014-11-15')
second occurrence
('User001',  '2014-11-15')
('User001',  '2014-11-22')
('User001',  '2014-11-22')
second occurrence
('User001',  '2014-11-22')
('User008',  '2014-11-22')
('User008',  '2014-11-22')
second occurrence
('User008',  '2014-11-22')

3 个答案:

答案 0 :(得分:2)

您可以使用cumcount查找第二次出现的所有索引:

mask = df.groupby(['user', 'date']).cumcount() == 1
idx = mask[mask].index
print (idx)
Int64Index([40, 200, 240, 440], dtype='int64')
for line in df.itertuples():
    print (line.user)
    print (line.date)
    if line.Index in idx:
        print ('second occurrence')

User001
2014-11-01
User001
2014-11-01
second occurrence
User001
2014-11-01
User001
2014-11-08
User001
2014-11-08
second occurrence
User001
2014-11-08
User001
2014-11-15
User001
2014-11-15
second occurrence
User001
2014-11-15
User001
2014-11-22
User001
2014-11-22
second occurrence
User001
2014-11-22

查找索引的另一个解决方案是:

idx = df[df.duplicated(['user', 'date']) & 
         df.duplicated(['user', 'date'], keep='last')].index
print (idx)
Int64Index([40, 200, 240, 440], dtype='int64')

答案 1 :(得分:1)

我建议使用DataFrame.duplicated() method来获取一个标识重复行的布尔索引。

根据您希望如何显示复制,您可以通过各种方式使用它,但如果您想迭代行并为每个行打印一个重复的通知,这样的事情可能会起作用:

duplicate_index = df.duplicates()
for row, dupl in zip(df, duplicate_index):
    print(row[0], row[1])
    if dupl:
        print('second occurrence')

答案 2 :(得分:1)

使用Counter跟踪

from collections import Counter

seen = Counter()
for i, row in df.iterrows():
    tup = tuple(row.values.tolist())
    if seen[tup] == 1:
        print(tup, '  second occurence')
    else:
        print(tup)
    seen.update([tup])

('User001', '2014-11-01')
('User001', '2014-11-01')   second occurence
('User001', '2014-11-01')
('User001', '2014-11-08')
('User001', '2014-11-08')   second occurence
('User001', '2014-11-08')
('User001', '2014-11-15')
('User001', '2014-11-15')   second occurence
('User001', '2014-11-15')
('User001', '2014-11-22')
('User001', '2014-11-22')   second occurence
('User001', '2014-11-22')
('User008', '2014-11-22')
('User008', '2014-11-22')   second occurence
('User008', '2014-11-22')