Home > Back-end >  Dataframe compare EQ - position doesn't matter
Dataframe compare EQ - position doesn't matter

Time:06-15

Reading this article - https://datatofish.com/compare-values-dataframes/ while helpful it doesn't help with my use-case in that I have multiple prices per product. ie product1 = computer has price = 350, 850.

My compare to DF has product2 = computer has price = 850, 350

When I compare these TWO since I think the order seems to matter it says that they do not match, how can I compare irregardless of order?

df.product_series.eq(other=df.warehouse_series)

sample dataframe

,Unnamed: 0,product,testfrom1,testfrom2,seriesfrom1,seriesfrom2
0,0,hi this is me,1703.0|1144.0|2172.0|735.0,,"['1703.0', '1144.0', '2172.0', '735.0']",
1,1,abc543,1120.0|637.0|2026.0|1599.0,,"['1120.0', '637.0', '2026.0', '1599.0']",
2,2,thisisus,2663.0|859.0|2281.0|1487.0,,"['2663.0', '859.0', '2281.0', '1487.0']",
3,3,abc123,1407.0|1987.0|696.0,,"['1407.0', '1987.0', '696.0']",
4,4,thing2,1392.0|1971.0|552.0,,"['1392.0', '1971.0', '552.0']",
5,5,thing1,1025.0|1566.0|581.0,,"['1025.0', '1566.0', '581.0']",

in the sample above I compare seriesfrom1 and seriesfrom2 but again I think the order where they differ is throwing things off..

CodePudding user response:

Use this custom function to compare. Can't compare it directly using eq

def custom_compare_eq(series,other):
    length = len(series.values)
    for i in range(length):
        r1 = eval(str(series.values[i]))
        r2 = eval(str(other.values[i]))
        
        if type(r1)!=type(r2):
            yield False
        else:
            if type(r1)==int:
                yield r1==r2
            elif type(r1)==list:
                yield set(r1)==set(r2)

result = list(custom_compare_eq(df.column1,df.column2))

This will compare two lists with different orders

  • Related