index Date Col_A Col_B Detection
0 1 Jan 0 1 0
1 2 Jan 0 0 0
2 3 Jan 1 0 0
3 4 Jan 0 1 1
4 5 Jan 0 0 0
5 6 Jan 1 0 0
6 7 Jan 0 0 0
7 8 Jan 0 0 1
8 9 Jan 0 0 0
9 10 Jan 0 0 0
10 11 Jan 0 0 1
我有上面的資料名。我想找到"Detection"和Col_A或Col_B列之間的相關性,如下所示:
遍歷Detectiondf1["Detection"]==1 的列,然后將其與 Col_A 的索引進行比較,如果df1["Col_A"]==1,則報告存在相關性 ( yes) 否則,shift(-2)如果存在與value ==1, 然后我們報告yeselse 報告No
以下是我的試用代碼
df1["Corr_with_A"] = np.where((df1['Col_A'] == 1 or df1['Col_A'].shif(-1) == 1 or df1['Col_A'].shif(-2) == 1) & (df1['Detection'] ==1), "Yes", "no")
df1["Corr_with_B"] = np.where((df1['Col_B'] == 1 or df1['Col_B'].shif(-1) == 1 or df1['Col_B'].shif(-2) == 1) & (df1['Detection'] ==1), "Yes", "no")
我的預期輸出(我想要我的輸出)
index Date Col_A Col_B Detection Corr_with_A Corr_with_B
0 1 Jan 0 1 0 no no
1 2 Jan 0 0 0 no no
2 3 Jan 1 0 0 no no
3 4 Jan 0 1 1 Yes Yes
4 5 Jan 0 0 0 no no
5 6 Jan 1 0 0 no no
6 7 Jan 0 0 0 no no
7 8 Jan 0 0 1 Yes no
8 9 Jan 0 0 0 no no
9 10 Jan 0 0 0 no no
10 11 Jan 0 0 1 no no
有人可以想出一個更好的方法來實作這一目標嗎?我的代碼給了我錯誤。謝謝你。
uj5u.com熱心網友回復:
這是一個很好的用例rolling.max:
N = 3 # number of rows to consider
m0 = df['Detection'].eq(1)
m1 = df['Col_A'].rolling(window=N, min_periods=1).max().eq(1)
m2 = df['Col_B'].rolling(window=N, min_periods=1).max().eq(1)
df['Corr_with_A'] = np.where(m0&m1, 'yes', 'no')
df['Corr_with_B'] = np.where(m0&m2, 'yes', 'no')
輸出:
index Date Col_A Col_B Detection Corr_with_A Corr_with_B
0 1 Jan 0 1 0 no no
1 2 Jan 0 0 0 no no
2 3 Jan 1 0 0 no no
3 4 Jan 0 1 1 yes yes
4 5 Jan 0 0 0 no no
5 6 Jan 1 0 0 no no
6 7 Jan 0 0 0 no no
7 8 Jan 0 0 1 yes no
8 9 Jan 0 0 0 no no
9 10 Jan 0 0 0 no no
10 11 Jan 0 0 1 no no
作為一個回圈:
N = 3 # number of rows to consider
m0 = df['Detection'].eq(1)
for col in ['A', 'B']:
m_rol = df[f'Col_{col}'].rolling(window=N, min_periods=1).max().eq(1)
df[f'Corr_with_{col}'] = np.where(m0&m_rol, 'yes', 'no')
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/450965.html
