How to add values into columns in pandas
Question:
Answers:
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
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