Home > Software design >  How to get percentage difference between two columns of different DataFrames?
How to get percentage difference between two columns of different DataFrames?

Time:11-28

There are 2 DataFrames with coin pairs and float prices. Need to make new DataFrame with coin pairs and the price difference as a percentage.

First DataFrame in txt

Second DataFrame in txt

I tried this function, it didn't work

def get_diff():
    for i in df2['askPrice']:
        for x in df3['Low price']:
            i = float(i)
            x = float(x)
            try:
                if i > x:
                    res = (round(i) - round(x)) / round(x) * 100
                    print(round(res))
                else:
                    print('lower')
            except ZeroDivisionError:
                print(float('inf'))
get_diff()

CodePudding user response:

Let's put the desired calcultaions in a new list then transform it into a column within df3

diff = [((y-x)/x)*100 for (y, x) in zip(df2['askPrice'],df3['Low price'])] 

df3['diff'] = diff
  • Related