How to reorder columns based on condition

I am new to programming. I am learning python and pandas from the internet.

I am working as a QA and trying to automate one of my manual work.

I have 2 datasets.

df1= pd.DataFrame{'number':['c-101','c-102','c-103'],'Summary':['Summary1','Summary2','Summary3']}

df2= pd.DataFrame{'ticket':['c-102','c-103','c-101'],'Summary':['Ticket1','Ticket2','Ticket3'], 'Priority':['High','Low','High']}

df2.ticket contains similar values as df1.numbers but it is shuffled.

df2 and df1 have different lengths with len(df1) > len(df2)

I am trying to create a new dataframe (df3) where :

  • if df1.number = df2.ticket exist : Update df3 with all the values in df1 (row) the corresponding df2.Priority (in a new column)
  • if df1.number = df2.ticket don’t exist : Update "NO PRIORITY"

So I need to loop through df1.number to check if each df2.ticket exist.

I tried:

for i in df2["ticket"]:
    if i in df1["number"]:
        ticket.append(df2["Priority"])
    else:
        ticket.append("NOT CREATED")

Output: df3 has all rows with Priority = "NOT CREATED" (even for df1.number = df2.ticket)

Really appreciate any input from community.

%d bloggers like this: