Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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_Pandas - Fatal编程技术网

Python 查找限制在多个范围内的重复项-熊猫

Python 查找限制在多个范围内的重复项-熊猫,python,pandas,Python,Pandas,假设我们的问题可以这样简化: df = pd.DataFrame() df['C_rows'] = ['C1', 'C2', 'C3', 'C2', 'C1', 'C2', 'C3', 'C1', 'C2', 'C3', 'C4', 'C1'] df['values'] = ['customer1', 4321, 1266, 5671, 'customer2', 123, 7344,'customer3', 4321, 4444, 5674, 'customer4'] C_rows

假设我们的问题可以这样简化:

df = pd.DataFrame()
df['C_rows'] = ['C1', 'C2', 'C3', 'C2', 'C1', 'C2', 'C3', 'C1', 'C2', 'C3', 'C4', 'C1']
df['values'] = ['customer1', 4321, 1266, 5671, 'customer2', 123, 7344,'customer3', 4321, 4444, 5674, 'customer4']
    C_rows  values
0   C1      customer1
1   C2      4321
2   C3      1266
3   C2      5671
与表格一起:

    C_rows  values
0   C1      customer1
1   C2      4321
2   C3      1266
3   C2      5671
4   C1      customer2
5   C2      123
6   C3      7344
7   C1      customer3
8   C2      4321
9   C3      4444
10  C4      5674
11  C1      customer4
我们如何在每个
C1
之间向量化查找重复的
C_行
, i、 e.
row3
在第1行和第3行中有重复的
C2
。 我正在处理的数据集有50000行,每个
C1
之间大约有15行

e、 g.检查如下重复项:

df = pd.DataFrame()
df['C_rows'] = ['C1', 'C2', 'C3', 'C2', 'C1', 'C2', 'C3', 'C1', 'C2', 'C3', 'C4', 'C1']
df['values'] = ['customer1', 4321, 1266, 5671, 'customer2', 123, 7344,'customer3', 4321, 4444, 5674, 'customer4']
    C_rows  values
0   C1      customer1
1   C2      4321
2   C3      1266
3   C2      5671
C2是重复的

没有重复的

没有重复的


如果不使用for循环,快速(矢量化)。

看起来像是
groupby
+
apply
(使用
复制的
)就可以了

df.groupby(df.C_rows.eq('C1').cumsum()).C_rows.apply(pd.Series.duplicated)

0     False
1     False
2     False
3      True
4     False
5     False
6     False
7     False
8     False
9     False
10    False
11    False
Name: C_rows, dtype: bool

使用遮罩过滤出
df

以获得非常快速的矢量化解决方案通过
C1
之间的连续值创建新的束,然后检查:

如果需要过滤器:

df = df[df.assign(dupe=df['C_rows'].eq('C1').cumsum()).duplicated(['C_rows','dupe'])]
print (df)
  C_rows values
3     C2   5671
如果要检查重复组:

df = df.assign(dupe=df['C_rows'].eq('C1').cumsum())
a = df.loc[df.duplicated(['C_rows','dupe']), 'dupe']
df['dupe'] = df['dupe'].isin(a)
print (df)
   C_rows     values   dupe
0      C1  customer1   True
1      C2       4321   True
2      C3       1266   True
3      C2       5671   True
4      C1  customer2  False
5      C2        123  False
6      C3       7344  False
7      C1  customer3  False
8      C2       4321  False
9      C3       4444  False
10     C4       5674  False
11     C1  customer4  False

您可以使用转换和复制,即

df['g'] = df['values'].astype(str).str.contains('[A-z]').cumsum()
df['is_dup'] = df.groupby('g')['C_rows'].transform(lambda x : x.duplicated().any())

  C_rows     values  g  is_dup
0      C1  customer1  1    True
1      C2       4321  1    True
2      C3       1266  1    True
3      C2       5671  1    True
4      C1  customer2  2   False
5      C2        123  2   False
6      C3       7344  2   False
7      C1  customer3  3   False
8      C2       4321  3   False
9      C3       4444  3   False
10     C4       5674  3   False
11     C1  customer4  4   False
如果只想找到重复的行,请删除
any()


在您的实际数据中,C_行中的值是什么样子的?大约100个单元格,具有不同的属性。是否要删除所有C2?或者保留第一个?我只是想确定它们,然后很可能保留第一个(需要首先分析我的数据集)C_行列组是否总是从
C1
开始?很好,只是我认为
groupby
这里的id应该比较慢。我认为组是从值开始的。我不认为小组总是从
C1
开始。例如,小组是从
C1
开始的,但是谢谢
df['g'] = df['values'].astype(str).str.contains('[A-z]').cumsum()
df['is_dup'] = df.groupby('g')['C_rows'].transform(lambda x : x.duplicated().any())

  C_rows     values  g  is_dup
0      C1  customer1  1    True
1      C2       4321  1    True
2      C3       1266  1    True
3      C2       5671  1    True
4      C1  customer2  2   False
5      C2        123  2   False
6      C3       7344  2   False
7      C1  customer3  3   False
8      C2       4321  3   False
9      C3       4444  3   False
10     C4       5674  3   False
11     C1  customer4  4   False
df['is_dup'] = df.groupby('g')['C_rows'].transform(lambda x : x.duplicated())