![](/img/trans.png)
[英]Make Pandas Dataframe column equal to value in another Dataframe based on index
[英]Calculate pandas dataframe index difference based on the value of another column
我試圖弄清楚如何計算當前行的索引與行WHERE某個列具有特定值的差異。
即
我有一個數據幀:
import pandas as pd
# pandas settings
pd.set_option('display.max_columns', 320)
pd.set_option('display.max_rows', 1320)
pd.set_option('display.width', 320)
df = pd.read_csv('https://www.dropbox.com/s/hy94jp4d7qwmv04/eurusd_df1.csv?dl=1')
所以我想計算一下后面有多少個索引是燭台= candle-20
因此,例如,如果當前行是583185並且蠟燭值是119,那么我們感興趣的蠟燭是99.我們需要計算出current_index - index(其中candle = 99 1st occurrence)
我希望自己清楚,干杯=)
編輯:好的,我上面做了很糟糕的解釋..
我相信我自己真的很接近解決這個問題。 看一看:
x = df.index[df.candle == df.candle - 20][0]
df['test'] = df.bid.rolling(int(x)).mean()
因此'test'列應該是df.bid最后X行的mean()值,其中X是當前df.candle和20燭光之間的行數(第一次迭代所以[0](那里)是多行具有相同的蠟燭值))
但上面的代碼給出了一個錯誤:
IndexError:索引0超出軸0的大小為0的范圍
這是一種實現此目的的方法:
# Generate example data
np.random.seed(0)
df = pd.Series(np.round(np.random.rand(1000000)*1000), dtype=int, name='candle').to_frame()
# Compute row index where df.candle is 20 less than candle_value at current_index
current_index = 583185
candle_value = df.loc[current_index, 'candle'] # = 119 in your df
index = df.index[df.candle == candle_value - 20][0]
print(index)
835
編輯:要計算索引的差異,只需減去它們:
X = current_index - index
print(X)
582350
然后你可以計算你的公式:
b = 0.015 * TP.rolling(X).std()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.