0
問題是在表中填充缺失值。在pandas,可以使用向前(或向後)填充這樣做,如下圖所示:在postgres中轉發(或向後填充)
$> import pandas as pd
$> df = pd.DataFrame({'x': [None, 1, None, None, 2, None, 3, None]})
$> df['y'] = df['x'].fillna(method='ffill')
$> df
x y
0 NaN NaN
1 1 1
2 NaN 1
3 NaN 1
4 2 2
5 NaN 2
6 3 3
7 NaN 3
有沒有辦法做到這一點的SQL和更精確的Postgres裏?我想window functions可以幫助,但我無法弄清楚如何。
Postgres裏,這將是這樣的:
sandbox=# SELECT x, ??
FROM
(SELECT NULL AS x
UNION ALL SELECT 1 AS x
UNION ALL SELECT NULL AS x
UNION ALL SELECT NULL AS x
UNION ALL SELECT 2 AS x
UNION ALL SELECT NULL AS x
UNION ALL SELECT 3 AS x
UNION ALL SELECT NULL AS x) a;
x
---
1
2
3
(8 rows)
這裏是表?你想填寫什麼樣的缺失值? –
我編輯它,我忘記了一個重要的熊貓行,它使用fillna向前填充,我想在postgres –
上重現,你將需要在這裏的功能 - 如果你只是'滯後'窗口功能 - 你會得到前一個x,螺母不在旁邊。換句話說,你需要某種類型的RECURSIVE LAG,重複以前的值,但是之前定義的值 –