Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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
如何识别excel单元格中的重复值,并使用python将其列在另一列中?_Excel_Python 3.x - Fatal编程技术网

如何识别excel单元格中的重复值,并使用python将其列在另一列中?

如何识别excel单元格中的重复值,并使用python将其列在另一列中?,excel,python-3.x,Excel,Python 3.x,我刚开始python编程,我被分配了一个excel任务。我必须在excel中的一列中识别重复的值(它大约有850行),然后在新列中列出它们。我是python的新手,因此不知道如何执行此任务。如果有人能分享如何开始这项任务或解决这项任务的任何方法,这将是非常有帮助的。一个示例代码也会非常有用。谢谢您可以使用熊猫,执行以下操作: import pandas as pd # let's assume this is our data which will be read from the exce

我刚开始python编程,我被分配了一个excel任务。我必须在excel中的一列中识别重复的值(它大约有850行),然后在新列中列出它们。我是python的新手,因此不知道如何执行此任务。如果有人能分享如何开始这项任务或解决这项任务的任何方法,这将是非常有帮助的。一个示例代码也会非常有用。谢谢

您可以使用熊猫,执行以下操作:

import pandas as pd 

# let's assume this is our data which will be read from the excel
rows = [ ( 'a' , 'b' , { 'a' , 'b' } ) , ( 'b' , 'c' , { 'b' , 'c' } ) 
    ( 'b' , 'a' , { 'a' , 'b' } ) ] 
columns = [ 'x' , 'y' , 'z' ] 

# create a dataframe (like a table in excel)
df = pd.DataFrame.from_records( rows , columns = columns) 

# you'll need to use: "pd.read_excel('my_excel_fle')" to read the file    

>> df

x  y       z
0  a  b  {b, a}
1  b  c  {b, c}
2  b  a  {b, a}

# now, put the columns which you want to find duplicates of in another column
df["dup_clmns"] = df.z.apply(lambda x: tuple(x))

>>df

x  y      z      dup_clmns

0  a  b  {b, a}  (b, a)
1  b  c  {b, c}  (b, c)
2  b  a  {b, a}  (b, a)

#now use "duplicates" or "drop_duplicates" (or whatever you want)
df.drop_duplicates(subset="dup_clmns", keep="first") # will remove duplicates of (x,y) columns

df.duplicated(subset="dup_clmns")
# will show duplicate rows based on the columns you choose