這裏是我的嘗試,具有一定的假設條件(加,藉此與一粒鹽:我並不算得上是大熊貓專家,只是認爲這將是一個有趣的問題上下工夫)
df = pandas.DataFrame([
{'date': '2010-01-01', 'stock_a': 10, 'stock_b': 20, 'stock_c': 30},
{'date': '2010-01-02', 'stock_a': 9.5, 'stock_b': 18, 'stock_c': 31},
{'date': '2010-03-01', 'stock_a': 10.5, 'stock_b': 22, 'stock_c': 39},
{'date': '2010-04-01', 'stock_a': 11.5, 'stock_b': 23, 'stock_c': 36},
])
# Assuming you want the first chronological record of such an event
df = df.sort_values(['date', 'stock_a', 'stock_b', 'stock_c'])
df2 = pandas.concat([
df['stock_a'] > df['stock_a'][0],
df['stock_b'] > df['stock_b'][0],
df['stock_c'] > df['stock_c'][0],
], axis=1)
df2['date'] = True
# At this point columns stock_{a,b,c} are populated with NaN in place where the value
# was less than the first observation
df3 = df[df2].query('stock_a == stock_a & stock_b == stock_b & stock_c == stock_c')
if not df3.empty:
print df3.iloc[[0]]
print 'No match'
注意:我編輯Stock-C是一個未被淘汰的股票。否則,所有3個被淘汰的股票和答案不能檢查未被淘汰的股票。 – JohnE
謝謝約翰,任何想法如何做到? –