0

I have a dataframe in which my data is in the type string - they're numbers but separated by commas. It looks something a bit like this.

>df
          1               2             3

1   728, 706, 687   699, 679, 658   714, 695, 675

To work with this data, I need to transform these string values into numbers in individual rows. Something a bit like this.

>df_new

    1     2    3

1  728   699  714
2  706   679  695
3  687   658  675

And I can't figure out how to do it for the life of me. Can anyone guide me on how to go about doing this?

Hamza Waheed
  • 139
  • 2
  • 10
  • 2
    Are you looking for `df.apply(lambda x: x.str.split(', ').explode())`? – cs95 Dec 25 '20 at 02:12
  • ok wow, that was fast. it worked like a charm, thanks a lot man – Hamza Waheed Dec 25 '20 at 02:14
  • 1
    An alternative to @cs95's solution : ``df.T[1].str.split(",", expand=True).T``. you could also check the performance. – sammywemmy Dec 25 '20 at 02:14
  • If my explode solution was helpful, please consider upvoting my answer [here](https://stackoverflow.com/a/57122617/4909087) which discusses this technique in more detail. – cs95 Dec 25 '20 at 02:28

0 Answers0