1

in sql request, i have a chance to sum with lagged param on the fly, like:

    CASE
       WHEN dt_rep.fcr = 0::numeric THEN 0::numeric
                    ELSE (dt_rep.fcr - lag(dt_rep.fcr) OVER (PARTITION BY dt_rep.place_id ORDER BY dt_rep.bucket)) / dt_rep.fcr
    END AS cr

here, I order the table by bucket (it is timeframe) and sorted them by place_id (to let group by place_id for lagging). Then, i can simply filtered the table with values of cr is null what gives me required structure.

in pandas, I can shift and assign new column like:

data_fin['fcr_b'] = data_fin.groupby(['place_id'])['fcr'].shift(1)

then I need to combine columns 'fcr_b' and 'fcr' to new one 'cr' and filter dataframe where fcr_b is NaN.

Is there more accurate approache to get new column 'cr' something like:

data_fin[cr] = data_fin['fcr'].apply(lambda x: x + lag(x).groupby('place_id'))

<= of cause, it is not working example!

1 Answer 1

1

Something like this?

df = pd.DataFrame({"A": [1, 1, 1, 2, 2, 2, 3, 3, 3], 
                   "B": ["a", "b", "c", "d", "e", "f", "g", "h", "i"], 
                   "C": ['text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text']})

df['D'] = df.groupby('A', as_index=False)['B'].shift(1).add(df['C'], fill_value=0)
df
Sign up to request clarification or add additional context in comments.

3 Comments

not exactly. sorry for not beeing clear with the question. In your example, if you set column [B] to numeric, column [C].shift(1) from [B] and to get column D as the sum of B+C. You don't need to fill NaN by anything. it is ok.
probably, i have to use iloc but I need an idea how to filter index by column (place_id)
@U12-Forward - yes, thank you!! to delete "fill_value=0" and it is exactly required structure!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.