Python/Pandas:跨列识别重复项

Python/Pandas:跨列识别重复项,python,pandas,duplicates,Python,Pandas,Duplicates,在下面的代码中,我想识别并报告Col1中出现在Col2中的值、Col2中出现在Col1中的值以及多次出现的总体值 在下面的示例中,值AAPL和GOOG出现在Col1和Col2中。在接下来的两列中,以及在后面的一列中,我们希望识别和报告Col1或Col2值中的“任何”是否为DUP import pandas as pd import numpy as np data={'Col1':['AAPL', np.nan, 'GOOG', 'MMM', np.nan, 'INTC', 'FB'],'Col

在下面的代码中,我想识别并报告Col1中出现在Col2中的值、Col2中出现在Col1中的值以及多次出现的总体值

在下面的示例中,值AAPL和GOOG出现在Col1和Col2中。在接下来的两列中,以及在后面的一列中,我们希望识别和报告Col1或Col2值中的“任何”是否为DUP

import pandas as pd
import numpy as np
data={'Col1':['AAPL', np.nan, 'GOOG', 'MMM', np.nan, 'INTC', 'FB'],'Col2':['GOOG', 'IBM', 'MSFT', np.nan, 'GOOG', 'AAPL', 'VZ']}
df=pd.DataFrame(data,columns=['Col1','Col2'])
print (df)

# How to code after this to produce expected result?
# Appreciate any hint/help provided

这里有一个解决方案,可用于上面的代码。它只使用了一些for循环和itetrows()。没什么特别的

df['Col3'] = False
df['Col4'] = False
df['Col5'] = False

for i,row in df.iterrows():
  if df.loc[i,'Col1'] in (df.Col2.values):
     df.loc[i,'Col3'] = True

for i,row in df.iterrows():
  if df.loc[i,'Col2'] in (df.Col1.values):
     df.loc[i,'Col4'] = True

for i,row in df.iterrows():
  if df.loc[i,'Col3'] | df.loc[i,'Col4'] == True:
     df.loc[i,'Col5'] = True

这里有一个解决方案,可用于上面的代码。它只使用了一些for循环和itetrows()。没什么特别的

df['Col3'] = False
df['Col4'] = False
df['Col5'] = False

for i,row in df.iterrows():
  if df.loc[i,'Col1'] in (df.Col2.values):
     df.loc[i,'Col3'] = True

for i,row in df.iterrows():
  if df.loc[i,'Col2'] in (df.Col1.values):
     df.loc[i,'Col4'] = True

for i,row in df.iterrows():
  if df.loc[i,'Col3'] | df.loc[i,'Col4'] == True:
     df.loc[i,'Col5'] = True

以下是最后的脚本:

##############################################################################
# Code to identify and report duplicates across columns
# np.nan values are handled
# Date: 04-JUL-2018
# Posted by: Salil V Gangal
# Forum: Stack OverFlow
##############################################################################

import pandas as pd
import numpy as np
data={'Col1':['AAPL', np.nan, 'GOOG', 'MMM', np.nan, 'INTC', 'FB'],'Col2':['GOOG', 'IBM', 'MSFT', np.nan, 'GOOG', 'AAPL', 'VZ']}
df=pd.DataFrame(data,columns=['Col1','Col2'])
print ("Initial DataFrame\n")
print (df)

pd.set_option("display.max_rows",999)
pd.set_option("display.max_columns",999)


df['Col1_val_exists_in_Col2'] = False
df['Col2_val_exists_in_Col1'] = False
df['Dup_in_Frame'] = False

for i,row in df.iterrows():
  if df.loc[i,'Col1'] in (df.Col2.values):
     df.loc[i,'Col1_val_exists_in_Col2'] = True

for i,row in df.iterrows():
  if df.loc[i,'Col2'] in (df.Col1.values):
     df.loc[i,'Col2_val_exists_in_Col1'] = True

for i,row in df.iterrows():
  if df.loc[i,'Col1_val_exists_in_Col2'] | df.loc[i,'Col2_val_exists_in_Col1'] == True:
     df.loc[i,'Dup_in_Frame'] = True

print ("Final DataFrame\n")
print (df)

以下是最后的脚本:

##############################################################################
# Code to identify and report duplicates across columns
# np.nan values are handled
# Date: 04-JUL-2018
# Posted by: Salil V Gangal
# Forum: Stack OverFlow
##############################################################################

import pandas as pd
import numpy as np
data={'Col1':['AAPL', np.nan, 'GOOG', 'MMM', np.nan, 'INTC', 'FB'],'Col2':['GOOG', 'IBM', 'MSFT', np.nan, 'GOOG', 'AAPL', 'VZ']}
df=pd.DataFrame(data,columns=['Col1','Col2'])
print ("Initial DataFrame\n")
print (df)

pd.set_option("display.max_rows",999)
pd.set_option("display.max_columns",999)


df['Col1_val_exists_in_Col2'] = False
df['Col2_val_exists_in_Col1'] = False
df['Dup_in_Frame'] = False

for i,row in df.iterrows():
  if df.loc[i,'Col1'] in (df.Col2.values):
     df.loc[i,'Col1_val_exists_in_Col2'] = True

for i,row in df.iterrows():
  if df.loc[i,'Col2'] in (df.Col1.values):
     df.loc[i,'Col2_val_exists_in_Col1'] = True

for i,row in df.iterrows():
  if df.loc[i,'Col1_val_exists_in_Col2'] | df.loc[i,'Col2_val_exists_in_Col1'] == True:
     df.loc[i,'Dup_in_Frame'] = True

print ("Final DataFrame\n")
print (df)

使用
numpy where
检查一列值是否在另一列中,然后使用布尔值或列来检查它是否为重复值

df['Col1inCol2']=np.where(df.Col1.isin(df.Col2) & ~df.Col1.isnull(), True, False)
df['Col2inCol1']=np.where(df.Col2.isin(df.Col1) & ~df.Col2.isnull(), True, False)
df['Dupe']= df.Col1inCol2 | df.Col2inCol1



    Col1    Col2    Col1inCol2  Col2inCol1  Dupe
0   AAPL    GOOG    True            True    True
1   NaN     IBM     False           False   False
2   GOOG    MSFT    True            False   True
3   MMM     NaN     False           False   False
4   NaN     GOOG    False           True    True
5   INTC    AAPL    False           True    True
6   FB       VZ     False           False   False

使用
numpy where
检查一列值是否在另一列中,然后使用布尔值或列来检查它是否为重复值

df['Col1inCol2']=np.where(df.Col1.isin(df.Col2) & ~df.Col1.isnull(), True, False)
df['Col2inCol1']=np.where(df.Col2.isin(df.Col1) & ~df.Col2.isnull(), True, False)
df['Dupe']= df.Col1inCol2 | df.Col2inCol1



    Col1    Col2    Col1inCol2  Col2inCol1  Dupe
0   AAPL    GOOG    True            True    True
1   NaN     IBM     False           False   False
2   GOOG    MSFT    True            False   True
3   MMM     NaN     False           False   False
4   NaN     GOOG    False           True    True
5   INTC    AAPL    False           True    True
6   FB       VZ     False           False   False

下面给出了完成任务的另一种方法-感谢“skrubber”:


下面给出了完成任务的另一种方法-感谢“skrubber”:


类似这样:
df['Col1inCol2']=np.where(df.Col1.isin(df.Col2),'True','False')
。你想把南斯也算进去吗?是的。np.nan不应算作DUP。查看Excel中的预期结果图像。col1中的
col2\u vals\u exist\u的第一个单元格显示
False
,这是为什么?暗:图像中有错误。应该是“真”而不是“假”;请参阅下面我的回答。类似这样的内容:
df['Col1inCol2']=np.where(df.Col1.isin(df.Col2),'True','False')
。你想把南斯也算进去吗?是的。np.nan不应算作DUP。查看Excel中的预期结果图像。col1中的
col2\u vals\u exist\u的第一个单元格显示
False
,这是为什么?暗:图像中有错误。应该是“真”而不是“假”;请看我下面的回复。谢谢“skrubber”。解决方案是有效的。而且它的代码比以前的解决方案更少!!我猜你需要点击答案旁边的绿色勾号,然后投票。谢谢“skrubber”。解决方案是有效的。而且它的代码比以前的解决方案更少!!我猜你需要点击答案旁边的绿色勾号,然后投票