When I was first starting out with Python, the below code was acceptable to me, because it got the job done. But currently re-writing and cleaning up this code for future use--is there a better way to make this more Pythonic via a loop or function?
movements['Agency.1_2'] = movements['Agency.1'] + ' to ' + movements['Agency.2']
movements['Agency.2_3'] = movements['Agency.2'] + ' to ' + movements['Agency.3']
movements['Agency.3_4'] = movements['Agency.3'] + ' to ' + movements['Agency.4']
movements['Agency.4_5'] = movements['Agency.4'] + ' to ' + movements['Agency.5']
movements['Agency.5_6'] = movements['Agency.5'] + ' to ' + movements['Agency.6']
movements['Agency.6_7'] = movements['Agency.6'] + ' to ' + movements['Agency.7']
movements['Agency.7_8'] = movements['Agency.7'] + ' to ' + movements['Agency.8']
movements['Agency.8_9'] = movements['Agency.8'] + ' to ' + movements['Agency.9']
movements['Agency.9_10'] = movements['Agency.9'] + ' to ' + movements['Agency.10']
Ultimately, this code spits out a bunch of new columns with concatenated strings with a ' to ' in the middle, e.g:
+-------+------------+------------+
| id | Agency.1_2 | Agency.2_3 |
+-------+------------+------------+
| 1 | a to b | b to c |
| 2 | b to d | f to g |
| 3 | z to y | |
+-------+------------+------------+
The current code works, so nbd if there isn't a better way. But would love to learn how to do this so I can push myself. Thanks!