Home > Enterprise >  Cyclic Shift (Rotate Rows) Dataframe with Groupy Pandas
Cyclic Shift (Rotate Rows) Dataframe with Groupy Pandas

Time:11-14

im trying to apply np.roll with groupy of pandas.

Actually, i have this dataframe: (In the columns complete_contracts_shift i applied shift with groupby:

df.groupby(['contracts ','param_contrct'])['complete_contracts '].shift(1)
complete_contracts contracts param_contrct complete_contracts_shift
F21-EZ-01/01/2022 F21 EZ NaN
F21-EZ-02/01/2022 F21 EZ F21-EZ-01/01/2022
F21-EZ-03/01/2022 F21 EZ F21-EZ-02/01/2022
F21-AB-01/01/2022 F21 AB NaN
F21-AB-02/01/2022 F21 AB F21-AB-01/01/2022
F21-AB-03/01/2022 F21 AB F21-AB-02/01/2022

I need that dataframe has the column 'complete_contracts_shift' as follows:

complete_contracts contracts param_contrct complete_contracts_shift
F21-EZ-01/01/2022 F21 EZ F21-EZ-03/01/2022
F21-EZ-02/01/2022 F21 EZ F21-EZ-01/01/2022
F21-EZ-03/01/2022 F21 EZ F21-EZ-02/01/2022
F21-AB-01/01/2022 F21 AB F21-AB-03/01/2022
F21-AB-02/01/2022 F21 AB F21-AB-01/01/2022
F21-AB-03/01/2022 F21 AB F21-AB-02/01/2022

i know np.roll of numpy, but i cant combinate this with groupby.

CodePudding user response:

If you are getting this result and consistently missing the third month. I suggest to go for a more simplistic approach with:

df['complete_contracts_shift'] = np.where(df['complete_contracts_shift'].isna(),df['contracts'] '-' df['param_contrct'] '-03/01/2022',df['complete_contracts_shift']

This will help you fill the NaN values with the correct combination of columns and date values.

The data you provide is pitifully not enough for me to give more a detailed answer.

CodePudding user response:

I could solve it with this code!

    _df['complete_contracts_shift'] = _df.groupby(['contracts','param_contrct'])['complete_contracts'].transform(lambda x: x.values[::-1])
  • Related