Home > Software engineering >  Find repeated rows in Excel and add into dictionary?
Find repeated rows in Excel and add into dictionary?

Time:01-03

I have 1 sheet with 2 columns

excel file

How can I find repeated rows in 'A' column

Add this rows in dictionary (Key = 'A' value & Value = 'B' value)

import pandas as pd
data = pd.read_excel (r'./1.xlsx')
df = pd.DataFrame(data, columns= ['xcode', 'xyear'])
print (df)

CodePudding user response:

To get duplicates in a dataframe you can use duplicated :

df["duplicated_flag"] = df.duplicated(subset=['xcode'])
# get the data that is duplicated only
duplicates = df[df["duplicated_flag"]]
# create a dictionary with the tuples of duplicates
result = dict(zip(duplicates["xcode"], duplicates["xyear"]))

CodePudding user response:

thank you , but i need this result

result image

  • Related