2016-06-18 63 views
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) 
+0

這裏是表?你想填寫什麼樣的缺失值? –

+0

我編輯它,我忘記了一個重要的熊貓行,它使用fillna向前填充,我想在postgres –

+0

上重現,你將需要在這裏的功能 - 如果你只是'滯後'窗口功能 - 你會得到前一個x,螺母不在旁邊。換句話說,你需要某種類型的RECURSIVE LAG,重複以前的值,但是之前定義的值 –

回答

1

window functions here

這麼多的別名,因爲你的查詢非常sencitive訂購。 我增加了更多的空x線,以證明它是修剪幾個空行...

select x,y from (
select r,x, case when y is not null then y else min(y) over (partition by x order by r) end y from (
SELECT row_number() over() r,x, case when x is not null then x else lag(x) over() end y 
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 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 
    ) b 
order by r 
    ) c 
    ; 

enter image description here