2017-11-04 5 views
1

私はDataFrame、Df2を持っています。私は、以下の列Lead_Lagのために、最後の10行のそれぞれをチェックしようとしている - これらの行のいずれかにNULL以外の任意の値がありますならば、私は'Y'に等しくなるように新しい列Positionをしたい:パンダ最終確認N値の行、結果に基づく新しい列

def run_HG_AUDUSD_15M_Aggregate(): 
    Df1 = pd.read_csv(max(glob.iglob(r"C:\Users\cost9\OneDrive\Documents\PYTHON\Daily Tasks\Pairs Trading\HG_AUDUSD\CSV\15M\Lead_Lag\*.csv"), key=os.path.getctime))  
    Df2 = Df1[['Date', 'Close_HG', 'Close_AUDUSD', 'Lead_Lag']] 

    Df2['Position'] = '' 

    for index,row in Df2.iterrows(): 
     if Df2.loc[Df2.index.shift(-10):index,"Lead_Lag"].isnull(): 
      continue 
     else: 
      Df2.loc[index, 'Position'] = "Y" 

サンプルを次のようにデータは次のとおりです。

Date \t Close_HG \t Close_AUDUSD \t Lead_Lag 
 
7/19/2017 12:59 \t 2.7 \t 0.7956 \t 
 
7/19/2017 13:59 \t 2.7 \t 0.7955 \t 
 
7/19/2017 14:14 \t 2.7 \t 0.7954 \t 
 
7/20/2017 3:14 \t 2.7 \t 0.791 \t 
 
7/20/2017 5:44 \t 2.7 \t 0.791 \t 
 
7/20/2017 7:44 \t 2.71 \t 0.7925 \t 
 
7/20/2017 7:59 \t 2.7 \t 0.7924 \t 
 
7/20/2017 8:44 \t 2.7 \t 0.7953 \t Short_Both 
 
7/20/2017 10:44 \t 2.71 \t 0.7964 \t Short_Both 
 
7/20/2017 11:14 \t 2.71 \t 0.7963 \t Short_Both 
 
7/20/2017 11:29 \t 2.71 \t 0.7967 \t Short_Both 
 
7/20/2017 13:14 \t 2.71 \t 0.796 \t Short_Both 
 
7/20/2017 13:29 \t 2.71 \t 0.7956 \t Short_Both 
 
7/20/2017 14:29 \t 2.71 \t 0.7957 \t Short_Both

したがって、この場合には、私は新しい列の最後の2つの値を望みます最後の10行のうちの少なくとも1つの行の値がLead_Lag列にあるので、'Y'になるようにしてください。私はロールベースでこれを適用したいと思います - 例えば、行13 '位置'の値は行12-3、行12を見ています。位置12 'エラー:

NotImplementedError: Not supported for type RangeIndex 

シフト方法のいくつかのバリエーション(ループの前に定義するなど)を試しても、動作させることはできません。

編集:

N = 10 
Df2['Position'] = '' 
for index,row in Df2.iterrows(): 
if (Df2.loc[index-N:index,"Lead_Lag"] != "N").any(): 
Df2.loc[index, 'Position'] = "Y" 
else: 
Df2.loc[index, 'Position'] = "N" 
+0

値がnull以外に存在する場合は、より」[ツアー] –

答えて

2

使用numpy.where連鎖することにより、ブールマスクで:ここに解決策だ

m = df["Lead_Lag"].notnull() & df.index.isin(df.index[-10:]) 

またはselect列での位置によってilocとし、reindexFalse秒を追加します。

m = df["Lead_Lag"].iloc[-10:].notnull().reindex(df.index, fill_value=False) 

df['new'] = np.where(m, 'Y', '') 

print (df) 
       Date Close_HG Close_AUDUSD Lead_Lag new 
0 7/19/2017 12:59  2.70  0.7956   NaN  
1 7/19/2017 13:59  2.70  0.7955   NaN  
2 7/19/2017 14:14  2.70  0.7954   NaN  
3 7/20/2017 3:14  2.70  0.7910   NaN  
4 7/20/2017 5:44  2.70  0.7910   NaN  
5 7/20/2017 7:44  2.71  0.7925   NaN  
6 7/20/2017 7:59  2.70  0.7924   NaN  
7 7/20/2017 8:44  2.70  0.7953 Short_Both Y 
8 7/20/2017 10:44  2.71  0.7964 Short_Both Y 
9 7/20/2017 11:14  2.71  0.7963 Short_Both Y 
10 7/20/2017 11:29  2.71  0.7967 Short_Both Y 
11 7/20/2017 13:14  2.71  0.7960 Short_Both Y 
12 7/20/2017 13:29  2.71  0.7956 Short_Both Y 
13 7/20/2017 14:29  2.71  0.7957 Short_Both Y 
+1

を参照してください学ぶために、質問に代わりにこの編集の答えとしてあなたのソリューションを追加してください、 'Y'を置くので、どちらかを変更する必要があります。 'isnull()'を 'notnull()'に変更するか、 'np.where'のパラメータを入れ替えます – GiantsLoveDeathMetal

+2

nit:最後に'、 'の後にスペースを入れてpep8に合致させます。 – GiantsLoveDeathMetal

+0

@GiantsLoveDeathMetalこれはデータフレーム/列全体に適用されますか?私は約8000行(上のサンプルだった)と私はあなたが与えた調整コードを実行した。ただし、最後の15行にのみ適用されます。 –

0

これは私がやってしまったものです:

def run_HG_AUDUSD_15M_Aggregate(): 


N = 10 
Df2['Position'] = '' 

for index,row in Df2.iterrows(): 
    if (Df2.loc[index-N:index,"Lead_Lag"] != "N").any(): 
     Df2.loc[index, 'Position'] = "Y" 
    else: 
     Df2.loc[index, 'Position'] = "N" 
関連する問題