我有一个大的数据框,其中一部分列是相同的 dup_columns = ['id', 'subject','topic', 'lesson', 'time']
,而另一些则是独特的 ['timestamps'].
id subj topic lesson timestamp time dup_ind dup_group time_diff
1 1 math add a timestamp1 45sec True 1 timestamp1-timestamp2
2 1 math add a timestamp2 45sec True 1 timestamp1-timestamp2
3 1 math add a timestamp2 30sec False NaN
4 1 math add a timestamp3 15sec False NaN
5 1 math add b timestamp1 0sec True 2 timestamp1-timestamp4
6 1 math add b timestamp4 0sec True 2 timestamp1-timestamp4
7 1 math add b timestamp1 45sec True 3 timestamp1-timestamp2
8 1 math add b timestamp2 45sec True 3 timestamp1-timestamp2
我有一列名为
['is_duplicate']
,它基于dup_columns
确定了重复项。我需要创建另一列['dup_group']
,通过分配唯一的重复组值(1,2,3 ...)来唯一标识每个重复的行集。最终,我需要使用dup_group
在每个重复组内比较timestamp
值(我使用.diff()方法实现此目的)。以下是我编写的代码:
df2= df1.loc[df1['is_duplicated']==True]
def dup_counter():
for name, group in df11.groupby(dup_columns):
df[name, df['dupsetnew']]+=1
return df['dupsetnew']
df11.groupby(dup_columns).apply(dup_counter)
问题1: 函数给我报错了(我是Python和编程的新手)
为了计算时间戳之间的差异,我有以下代码:
df['time_diff'] = df.loc[df.dup_indicator == 1,'event_time'].diff()
问题2:对于我需要的内容,.diff是正确的方法吗?