1

In my infinite quest of teaching myself program, I have come to find that the easiest questions to ask about doing something is the HARDEST to grasp (programming-wise).

Case in point:

Let's say I have an output from printing out a data frame:

Work Date   Sequence Numbers      
01/01/2023  01 02 03 04 05 06  
02/01/2023  07 08 09 10 11 12      
03/01/2023  13 14 15 16 17 18

I have (slowly) learned how to use iloc in pulling out data from the series and retrieving values from the index. However, I am hitting a wall in pulling the ACTUAL data from within the series to manipulate.

For example - in the output above, I'd love to be able to pull out the value of 1 2 3 or remove the last values and put that in its own series. Or maybe pull out all of the values and give them its own column.

My first thought was using regular python slicing techniques, however, I'm either not getting it or it doesn't work well with Pandas when I use it. Lastly, I've tried searching all over YouTube and Stack Overflow for extracting ACTUAL data, however, it's yielded nothing to what I'm looking for.

Thoughts?

Ian Thompson
  • 2,914
  • 2
  • 18
  • 31
VJ1222
  • 21
  • 1

1 Answers1

2

IIUC, you want to split Sequence Numbers column like:

df = df.join(df['Sequence Numbers'].str.split('\s+', expand=True).add_prefix('S'))
print(df)

# Output
    Work Date   Sequence Numbers  S0  S1  S2  S3  S4  S5
0  01/01/2023  01 02 03 04 05 06  01  02  03  04  05  06
1  02/01/2023  07 08 09 10 11 12  07  08  09  10  11  12
2  03/01/2023  13 14 15 16 17 18  13  14  15  16  17  18
Corralien
  • 109,409
  • 8
  • 28
  • 52
  • That is pretty much it, that's incredible! At least I don't feel like a complete fool in that there are commands that differ from slicing lists. (i.e. join). That is great. – VJ1222 Mar 02 '23 at 16:51
  • You have a column with a string. We slice it according to whitespace and tell Pandas to expand the list into as many columns as needed. We add a prefix to the column name '0' -> 'S0', '1' -> 'S1', etc. After that we have another dataframe that we want to join with your initial one. – Corralien Mar 02 '23 at 17:04