在Pandas数据框中使用groupby时,如何根据上一行的值增加一列?

时间:2019-03-17 02:55:55

标签: python pandas dataframe

我有以下数据框:

claim   diagnosis   sequence
100     1           1.0
100     2           1.0
100     3           NaN
100     4           NaN
105     1           1.0
105     2           2.0
105     3           2.0
105     4           NaN
111     1           1.0
111     2           2.0
111     3           3.0
111     4           NaN

我需要的是通过声明将所有NaN替换为上一行中的oneup值:

claim   diagnosis   sequence
100     1           1.0
100     2           1.0
100     3           2.0
100     4           3.0
105     1           1.0
105     2           2.0
105     3           2.0
105     4           3.0
111     1           1.0
111     2           2.0
111     3           3.0
111     4           4.0

我尝试过cumcount,但似乎无法使用以前的值。我也尝试过loc,但还不太熟悉。

things = [{'claim':100, 'diagnosis':1, 'sequence':1},
    {'claim':100, 'diagnosis':2, 'sequence':1 },
    {'claim':100, 'diagnosis':3, },
    {'claim':100, 'diagnosis':4, },
    {'claim':105, 'diagnosis':1, 'sequence':1},
    {'claim':105, 'diagnosis':2, 'sequence':2},
    {'claim':105, 'diagnosis':3,'sequence':2 },
    {'claim':105, 'diagnosis':4, },
    {'claim':111, 'diagnosis':1, 'sequence':1},
    {'claim':111, 'diagnosis':2, 'sequence':2},
    {'claim':111, 'diagnosis':3,  'sequence':3},
    {'claim':111, 'diagnosis':4, }]

df = pd.DataFrame(things)
df

我已经为此动了好几天,任何帮助都是很棒的。

1 个答案:

答案 0 :(得分:1)

使用libname hs2 SPDE '/work/saswork/spde'; DATA table1 (KEEP=cp b pl ii ex exy fred sta n ee qwe ver pot gr pcr burb) table3 (KEEP=b pt cl sb n sd ed yt ta cl sb cp pcr burb) table2 (KEEP=b pt cl sb n sd ed yt ta cl sb cp pcr burb); SET hs2.bigtable_char ( KEEP=cp b pl ii ex exy fred sta n ee qwe ver poy gr pcr burb pt cl sb sd ed yt ta WHERE=(cl= 'YTR' and sb = 'IYU' and cp in (2,47,101,45))); IF (sd <= "&end."d <= ed AND ti /*ti is a numeric variable*/ AND ta not in ('A' 'B' 'C')) THEN OUTPUT table3; IF (sd <= "&middle."d <= ed AND ti AND ta not in ('A' 'B' 'C')) THEN OUTPUT table2; IF (fred >= "&start."d and fred <= "&end."d) THEN OUTPUT table1; RUN; 计数该行之前的cumsum数,然后加NaN

ffill