可以說我有資料框:
| 指數 | X |
|---|---|
| 0 | 5 |
| 1 | 20 |
| 2 | 25 |
| 3 | 50 |
| 4 | 10 |
| 5 | 11 |
| 6 | 18 |
| 7 | 28 |
| 8 | 22 |
| 9 | 55 |
| 10 | 40 |
我想為 x 列中的每個值/索引獲取前 5 個值,因此預期輸出為:
| 指數 | X | 預期輸出 |
|---|---|---|
| 0 | 5 | [null null null null null ] |
| 1 | 20 | 無 無 無 無 5 |
| 2 | 25 | 無 無 無 5 20 |
| 3 | 50 | 空空 5 20 25 |
| 4 | 10 | 無 5 20 25 50 |
| 5 | 11 | 5 20 25 50 10 |
| 6 | 18 | 20 25 50 10 11 |
| 7 | 28 | 25 50 10 11 18 |
| 8 | 22 | 50 10 11 18 28 |
| 9 | 55 | 10 11 18 28 22 |
| 10 | 40 | 11 18 28 22 55 |
我用過這個 iloc
for index, row in df.iterrows():
print(index,row['x'],df.iloc[-6:-1,2].values)
有沒有類似的 iloc 可以解決這個方法?
uj5u.com熱心網友回復:
使用帶有附加缺失值的步幅:
n = 5
x = np.concatenate([[np.nan] * (n), df['x']])
def rolling_window(a, window):
shape = a.shape[:-1] (a.shape[-1] - window 1, window)
strides = a.strides (a.strides[-1],)
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
df['expected_output'] = rolling_window(x, n)[:-1].tolist()
print (df)
x expected_output
index
0 5 [nan, nan, nan, nan, nan]
1 20 [nan, nan, nan, nan, 5.0]
2 25 [nan, nan, nan, 5.0, 20.0]
3 50 [nan, nan, 5.0, 20.0, 25.0]
4 10 [nan, 5.0, 20.0, 25.0, 50.0]
5 11 [5.0, 20.0, 25.0, 50.0, 10.0]
6 18 [20.0, 25.0, 50.0, 10.0, 11.0]
7 28 [25.0, 50.0, 10.0, 11.0, 18.0]
8 22 [50.0, 10.0, 11.0, 18.0, 28.0]
9 55 [10.0, 11.0, 18.0, 28.0, 22.0]
10 40 [11.0, 18.0, 28.0, 22.0, 55.0]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/444916.html
