在数据帧中循环时计算行的出现次数

2024-06-25 22:59:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧,每行重复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')

Tags: 数据in内容dffordatelinedo
3条回答

使用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')

我建议使用^{} method获得一个布尔索引,标识重复的行。你知道吗

根据您希望如何显示副本,您可以以各种方式使用它,但是如果您希望迭代行并为每个副本打印通知,类似的操作可能会起作用:

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

可以使用^{}查找第二次出现的所有索引:

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')

相关问题 更多 >