-1

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.

  • 1
    Should Jimmy get the values from Anton (rotating the values) or NaNs (shifting the values)? Please [edit] to show your desired output, for completeness. If you're talking about shifting, there are existing questions about that, like [Shift column in pandas dataframe up by one?](/q/20095673/4518341) – wjandrea Mar 06 '23 at 22:28
  • 1
    [Don't post pictures of data](https://meta.stackoverflow.com/q/285551/4518341). Instead, copy the text itself, [edit] it into your post, and use the formatting tools like [code formatting](/editing-help#code). Or you could use [table formatting](/editing-help#tables) and put the new values in italics or something. – wjandrea Mar 06 '23 at 22:31
  • 1
    Does this answer your question? [Shift column in pandas dataframe up by one?](https://stackoverflow.com/questions/20095673/shift-column-in-pandas-dataframe-up-by-one) – Erik Fubel Mar 06 '23 at 22:38

1 Answers1

0

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
Erik Fubel
  • 661
  • 1
  • 15
  • @WilliamAshoti You're welcome. Please consider the comments under your question to improve this question and apply the suggestions to future questions. This makes it easier for other users to help you and decreases the number of duplicate questions :) – Erik Fubel Mar 06 '23 at 22:52