How to add values into columns in pandas

enter image description here

How can I add the values from column X and Y to X2 and Y2 using pandas dataframework? For example I want the X and Y value of Brad to be added into the X2 and Y2 column of Anton and for Mikel to be added to X2 and Y2 of Brad and so on till the end.

>Solution :

You are looking for Series.shift():

df['X2'] = df['X'].shift(-1)
df['Y2'] = df['Y'].shift(-1)

Note that the last row will be NaN:

  Player     X     Y    X2    Y2
0  Anton  49.5  50.5  36.4  44.5
1   Brad  36.4  44.5  20.3  30.7
2  Mikel  20.3  30.7  10.0  44.4
3  Jimmy  10.0  44.4   NaN   NaN

Leave a Reply