Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading