Luckasino
Luckasino

Reputation: 424

Shift only selected rows in Pandas

I would like to shift only specific rows in my DataFrame by 1 period on the columns axis.

Df
Out:
  Month Year_2005 Year_2006 Year_2007
0 01    NaN       31        35 
1 02    NaN       40        45
2 03    NaN       87        46    
3 04    NaN       55        41
4 05    NaN       36        28
5 06    31        21        NaN
6 07    29        27        NaN

To have something like this:

Df
Out:
  Month Year_2005 Year_2006 Year_2007
0 01    NaN       31        35 
1 02    NaN       40        45
2 03    NaN       87        46    
3 04    NaN       55        41
4 05    NaN       36        28
5 06    NaN       31        21
6 07    NaN       29        27

My code so far:

rows_to_shift = Df[Df['Year_2005'].notnull()].index
Df.iloc[rows_to_shift, 1] = Df.iloc[rows_to_shift,2].shift(1)

Upvotes: 0

Views: 184

Answers (2)

SeaBean
SeaBean

Reputation: 23217

You can try:

df1 = df.set_index('Month')
df1 = df1.apply(lambda x: pd.Series(sorted(x, key=pd.notna), index=x.index), axis=1)
df = df1.reset_index()

Result:

   Month  Year_2005  Year_2006  Year_2007
0      1        NaN       31.0       35.0
1      2        NaN       40.0       45.0
2      3        NaN       87.0       46.0
3      4        NaN       55.0       41.0
4      5        NaN       36.0       28.0
5      6        NaN       31.0       21.0
6      7        NaN       29.0       27.0

Upvotes: 1

not_speshal
not_speshal

Reputation: 23146

Try:

df = df.set_index("Month")
df[df["Year_2005"].notnull()] = df[df["Year_2005"].notnull()].shift(axis=1)

>>> df
       Year_2005  Year_2006  Year_2007
Month                                 
1            NaN       31.0       35.0
2            NaN       40.0       45.0
3            NaN       87.0       46.0
4            NaN       55.0       41.0
5            NaN       36.0       28.0
6            NaN       31.0       21.0
7            NaN       29.0       27.0

Upvotes: 1

Related Questions