我有一个像这样的 Pandas DataFrame(这是一个三角套利问题)
>>> df_diff_rel
a b c d
a -0.833333 -1.666667 -2.500000 0.833333
b 0.000000 -0.840336 -1.680672 1.680672
c -1.652893 -2.479339 -3.305785 0.000000
d -2.459016 -3.278689 -4.098361 -0.819672
我需要知道正值的行名和列名(从最大值到最小值排序)
在我的例子中,我想得到
1.680672 b d
0.833333 a d
0 b a
0 c d
我做了这个
lst_arbitrage_opportunities = list(df_diff_rel[df_diff_rel>0])
lst_arbitrage_opportunities.sort(reverse=True)
但现在我不知道如何获取给定值的行名和列名。
这是获取我的示例的完整代码:
import pandas as pd
import numpy as np
class Ticker:
def __init__(self, ask=None, bid=None):
self.ask = ask
self.bid = bid
def spread(self):
return(self.ask-self.bid)
def __repr__(self):
str = """ask: {ask}
bid: {bid}""".format(ask=self.ask, bid=self.bid)
if self.ask!=None and self.bid!=None:
str = str + """
spread: {spread}""".format(spread=self.spread())
return(str)
markets = ['a', 'b', 'c', 'd']
markets_tickers = dict()
markets_tickers['a'] = Ticker(1.20, 1.19)
markets_tickers['b'] = Ticker(1.19, 1.18)
markets_tickers['c'] = Ticker(1.21, 1.17)
markets_tickers['d'] = Ticker(1.22, 1.21)
df_ask = pd.DataFrame(index=markets, columns=markets)
df_bid = pd.DataFrame(index=markets, columns=markets)
size = 1.0
for mk in markets:
print("="*5+mk+"="*5)
print(markets_tickers[mk])
df_ask.ix[mk]=markets_tickers[mk].ask
df_bid[mk]=markets_tickers[mk].bid
df_diff_abs = (df_bid - df_ask)*size
df_diff_rel = (df_bid - df_ask)/df_ask*100.0
df_arbitrage_opportunities = df_diff_rel>0
lst_arbitrage_opportunities = list(df_diff_rel[df_diff_rel>0])
lst_arbitrage_opportunities.sort(reverse=True)
print("Ask")
print(df_ask)
print("Bid")
print(df_bid)
print("Diff abs")
print(df_diff_abs)
print("Diff rel")
print(df_diff_rel)
print("Arbitrage opportunities")
print(df_arbitrage_opportunities)
print("List of opportunities (from the best to the worst)")
print(lst_arbitrage_opportunities)