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

Expand distance between values in pandas dataframe

How to expand distance between values in pandas dataframe?

  A
1 3
2 5
3 6
5 5
6 9

I want to increase the distance between adjacent elements by x times, for example by two times.

Expected output:

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

  A B
1 3 3
2 5 7  # (3 + 2 * 2)
3 6 9  # (7 + 1 * 2)
5 5 7  # (9 - 1 * 2)
6 9 15 # (7 + 4 * 2)

>Solution :

Solution from comment – multiple A by 2 and subtract first value:

df['B'] = df['A'].mul(2) - df['A'].iat[0]
print (df)
   A   B
1  3   3
2  5   7
3  6   9
5  5   7
6  9  15
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