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]:
a a -0.833333
b 0.000000
c -1.652893
d -2.459016
b a -1.666667
b -0.840336
c -2.479339
...
You can then filter this list like so to find values >= 0 :
In [3]: df.unstack()[df.unstack() >= 0]
Out[3]:
a b 0.000000
d a 0.833333
b 1.680672
c 0.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"