Home > OS >  Is there a way to print a string into a new df column based off of the other values of two other col
Is there a way to print a string into a new df column based off of the other values of two other col

Time:12-23

My desired output would be the highlighted column

enter image description here

CodePudding user response:

You can use ord to convert the letters to numbers, range to generate a range of all the numbers in between, and chr to convert the numbers back to letters. Then use df.apply() with a lambda function and axis=1 to do this for every row:

df['Final'] = df.apply(lambda x: ' '.join(chr(c) for c in range(ord(x['Letter start']), ord(x['Letter finish'])   1)), axis=1)

Output:

>>> df
  Letter start Letter finish    Final
0            A             D  A B C D
1            C             E    C D E
2            K             M    K L M
  • Related