Home > Enterprise >  How to get the duplicates from the lists based on name
How to get the duplicates from the lists based on name

Time:12-21

I am accessing some files from the server and printing the results but how can I get the duplicates based on the name from the printed results.

from datetime import datetime
class EsriApiMaps:

    def __init__(self, portal, item_type, query):
        self.item_type = item_type
        self.query_ = query
        self.portal = portal

    def query_maps(self):
        api_query_result = self.portal.content.search(query=self.query_, item_type=self.item_type)
        l = [] # we will store all the services e,g url,id,owner etc


        for l in api_query_result:
            l_created_time = datetime.fromtimestamp(round(l.created / 1000))
            l_modified_time = datetime.fromtimestamp(round(l.modified / 1000))
            df = ("Name: "   l.title   "ID: "   l.id   ", Owner: "   l.owner   ", Created: "   str(l_created_time)   ", Modified: "   str(l_modified_time))
            print(df)

I have tried this to get the below results,

Name: KL, ID: af57c454, Owner: Scripter, Created: 2019-10-08 12:57:45, Modified: 2019-10-08 12:57:45
Name: KL, ID: dfsjd5s4, Owner: d011, Created: 2020-10-27 21:02:54, Modified: 2020-10-27 21:02:54
Name: TEAM, ID: b8djx8, Owner: j277, Created: 2019-10-08 12:52:54, Modified: 2019-10-08 12:52:54
Name: ALL, ID: b896sfd, Owner: rp10, Created: 2019-10-11 14:51:38, Modified: 2019-10-11 14:51:38
Name: MD, ID: dhx865, Owner: ws07, Created: 2019-10-08 15:17:59, Modified: 2019-10-08 15:17:59
Name: AJKL, ID: dhsa88, Owner: fsdd, Created: 2020-07-23 16:04:20, Modified: 2020-07-23 16:04:20
Name: MD, ID: sd5425, Owner: fsdd, Created: 2021-02-02 11:43:15, Modified: 2021-02-02 11:43:15
Name: MD, ID: vcxb65, Owner: dsff1, Created: 2020-06-17 10:56:36, Modified: 2020-06-17 10:56:36

I have tried using,

names = df.Name.value_counts()
names[names>1] 

But I am getting this error AttributeError: 'str' object has no attribute 'Name'

How can I get the duplicates based on its name ?

The expected result is

Name: KL, ID: af57c454, Owner: Scripter, Created: 2019-10-08 12:57:45, Modified: 2019-10-08 12:57:45
Name: KL, ID: dfsjd5s4, Owner: d011, Created: 2020-10-27 21:02:54, Modified: 2020-10-27 21:02:54
Name: MD, ID: sd5425, Owner: fsdd, Created: 2021-02-02 11:43:15, Modified: 2021-02-02 11:43:15
Name: MD, ID: vcxb65, Owner: dsff1, Created: 2020-06-17 10:56:36, Modified: 2020-06-17 10:56:36
Name: MD, ID: dhx865, Owner: ws07, Created: 2019-10-08 15:17:59, Modified: 2019-10-08 15:17:59

CodePudding user response:

df it's a string, not a dataframe, you should create a dataframe with the results of the API query and then you could use de dataframe methods.

You can create a list with the results and then initialize the dataframe witht it.

def query_maps(self):
        api_query_result = self.portal.content.search(query=self.query_, item_type=self.item_type)
        data = []

        for l in api_query_result:
            l_created_time = datetime.fromtimestamp(round(l.created / 1000))
            l_modified_time = datetime.fromtimestamp(round(l.modified / 1000))
            data.append({"Name": l.title, "ID": l.id, "Owner": l.owner, "Created": str(l_created_time), "Modified": str(l_modified_time)})
        df = pd.Dataframe(data)
  • Related