如何在一个时间范围内按多列查找重复行
How to find duplicate rows by multiple columns and within a timeframe
对于下面的示例数据框,我试图获取 'fruit' 和 'animal' 列具有相同值的行 和 的区别dateTime 列的值不超过 10 分钟,但我在最后一步遇到问题。 (有关更多详细信息,所选行最终将进入单独的 df 并从当前 df 中删除)。
df_test:
dateTime fruit animal number
1 08/01/2020 1:08:00 AM apple monkey 1
2 08/01/2020 1:05:00 AM apple monkey 4
3 08/01/2020 1:20:00 AM apple frog 3
4 08/01/2020 1:40:00 AM pear dog 1
5 08/01/2020 1:47:00 AM apple monkey 2
为了获取'fruit 和 'animal' 都匹配的行,我尝试了:
duplicates_df = df_test[df_test.duplicated(['fruit','animal'])]
将重复项放入 duplicates_df 后,我无法找到可以使用哪些方法来提取适用于有关 dateTime 的指定规则的方法。解决这个问题的方法是什么?
我不确定这是否回答了您的问题
df.sort_values(by=['dateTime'], inplace=True)
cond = ((df[['fruit', 'animal']] == df[['fruit', 'animal']].shift()).all(axis=1)) & df.dateTime.diff().lt('10min')
df[~cond]
试试这个
from datetime import timedelta
import itertools as itt
def processGroup(G):
indexes = G.index.to_list()
groups = []
for i1,i2 in itt.combinations(indexes, 2):
added=False
if (max(df.dateTime[i1] , df.dateTime[i2]) - min(df.dateTime[i1] , df.dateTime[i2])).seconds/60 <= 20.0:
for g in groups:
if (i1 in g) and (i2 not in g):
g.append(i2)
added=True
break
elif (i2 in g) and (i1 not in g):
g.append(i1)
added=True
break
elif (i2 in g) and (i1 in g):
added=True
break
if not added:
groups.append([i1, i2])
# print(groups)
G['Group'] = ''
result = pd.DataFrame(columns=G.columns.to_list())
for i, g in enumerate(groups):
result = pd.concat([result, G.loc[g]])
result.loc[g, 'Group'] = (i+1)
return result
RESULT = pd.DataFrame(columns=df.columns.to_list()+['Group'])
for i, g in df.groupby(['fruit', 'animal']):
# print(g)
RESULT = pd.concat([RESULT, processGroup(g)])
print(RESULT.set_index(['fruit', 'animal', 'Group']))
对于下面的示例数据框,我试图获取 'fruit' 和 'animal' 列具有相同值的行 和 的区别dateTime 列的值不超过 10 分钟,但我在最后一步遇到问题。 (有关更多详细信息,所选行最终将进入单独的 df 并从当前 df 中删除)。
df_test:
dateTime fruit animal number
1 08/01/2020 1:08:00 AM apple monkey 1
2 08/01/2020 1:05:00 AM apple monkey 4
3 08/01/2020 1:20:00 AM apple frog 3
4 08/01/2020 1:40:00 AM pear dog 1
5 08/01/2020 1:47:00 AM apple monkey 2
为了获取'fruit 和 'animal' 都匹配的行,我尝试了:
duplicates_df = df_test[df_test.duplicated(['fruit','animal'])]
将重复项放入 duplicates_df 后,我无法找到可以使用哪些方法来提取适用于有关 dateTime 的指定规则的方法。解决这个问题的方法是什么?
我不确定这是否回答了您的问题
df.sort_values(by=['dateTime'], inplace=True)
cond = ((df[['fruit', 'animal']] == df[['fruit', 'animal']].shift()).all(axis=1)) & df.dateTime.diff().lt('10min')
df[~cond]
试试这个
from datetime import timedelta
import itertools as itt
def processGroup(G):
indexes = G.index.to_list()
groups = []
for i1,i2 in itt.combinations(indexes, 2):
added=False
if (max(df.dateTime[i1] , df.dateTime[i2]) - min(df.dateTime[i1] , df.dateTime[i2])).seconds/60 <= 20.0:
for g in groups:
if (i1 in g) and (i2 not in g):
g.append(i2)
added=True
break
elif (i2 in g) and (i1 not in g):
g.append(i1)
added=True
break
elif (i2 in g) and (i1 in g):
added=True
break
if not added:
groups.append([i1, i2])
# print(groups)
G['Group'] = ''
result = pd.DataFrame(columns=G.columns.to_list())
for i, g in enumerate(groups):
result = pd.concat([result, G.loc[g]])
result.loc[g, 'Group'] = (i+1)
return result
RESULT = pd.DataFrame(columns=df.columns.to_list()+['Group'])
for i, g in df.groupby(['fruit', 'animal']):
# print(g)
RESULT = pd.concat([RESULT, processGroup(g)])
print(RESULT.set_index(['fruit', 'animal', 'Group']))