Skip to content Skip to sidebar Skip to footer

How To Use Python And Pandas To Find Bests Opportunities In Triangular Arbitrage

I have a Pandas DataFrame like this (it's a triangular arbitrage problem) >>> df_diff_rel a b c d a -0.833333 -1.666667 -2.500000 0.8333

Solution 1:

Here's a simple one-line solution:

To get the data in the shape you want you can use the unstack method:

In[2]: df.unstack()
Out[2]:
aa-0.833333b0.000000c-1.652893d-2.459016ba-1.666667b-0.840336c-2.479339
...

You can then filter this list like so to find values >= 0 :

In[3]: df.unstack()[df.unstack() >= 0]Out[3]:
ab0.000000da0.833333b1.680672c0.000000

Finally, you can access the index of the above object to return a list of labels:

In [1]: df.unstack()[df.unstack() >= 0].index.tolist()
Out[1]: [('a', 'b'), ('d', 'a'), ('d', 'b'), ('d', 'c')]

Update:

To sort in descending order use the Series.order method instead of sort:

In [1]: tmp = df.unstack()[df.unstack() >= 0]

In [2]: tmp = tmp.order(ascending=False)

In [3]: tmp
Out[3]:
d  b    1.680672
   a    0.833333
   c    0.000000
a  b    0.000000

In [4]: tmp.index.tolist()
Out[4]: [('d', 'b'), ('d', 'a'), ('d', 'c'), ('a', 'b')]

Post a Comment for "How To Use Python And Pandas To Find Bests Opportunities In Triangular Arbitrage"