0

This is my current line:

  tableau_indic_final = pd.concat( [tableau_indic['code_Mono'], tableau_indic['Nom_medicament'], tableau_indic['Indications'].str.split(';', expand=True).add_prefix('Indication')], axis=1 )

I realized afterwards that I also need to split when there is a ","

So I want the split to happen every time I encounter a ";" OR a ","

marou95thebest
  • 189
  • 2
  • 10
  • 2
    Does this answer your question? [Split Strings into words with multiple word boundary delimiters](https://stackoverflow.com/questions/1059559/split-strings-into-words-with-multiple-word-boundary-delimiters) – rmssoares Nov 22 '20 at 23:25
  • What does `expand=True` do in `str.split(';', expand=True)`? I checked [the official python docs](https://docs.python.org/3/library/stdtypes.html#str.split), and there is no mention of this. – Ayush Garg Nov 23 '20 at 01:34

2 Answers2

3

you can use this:

import re
new_list = re.split(';|,',your_string)

assume that you want to split your string with : then you use:

new_list = re.split(';|,|:',your_string)

... etc

buddemat
  • 4,552
  • 14
  • 29
  • 49
  • Thanks you I'm going to try it like this. – marou95thebest Nov 22 '20 at 23:33
  • I tried this but it did not work `tableau_indic_final = pd.concat( [tableau_indic['code_Mono'], tableau_indic['Nom_medicament'], tableau_indic['Indications'].str.split(r';|,', expand=True).add_prefix('Indication')], axis=1 )` – marou95thebest Nov 23 '20 at 01:03
  • I also tried `tableau_indic_final = pd.concat( [tableau_indic['code_Mono'], tableau_indic['Nom_medicament'], tableau_indic['Indications'].str.split(r';|,', expand=True).add_prefix('Indication')], axis=1 )` but I get also an error – marou95thebest Nov 23 '20 at 01:05
  • @marou95thebest you need to use `re.split` on the string - `tableau_indic_final = pd.concat( [tableau_indic['code_Mono'], tableau_indic['Nom_medicament'], re.split(';|,', tableau_indic['Indications'].str).add_prefix('Indication')], axis=1 )`. – Ayush Garg Nov 23 '20 at 01:33
  • This time I have `TypeError: expected string or bytes-like object` – marou95thebest Nov 23 '20 at 03:05
1

In case you don't want to use regex, you could always replace ; with , and then split on , (or the other way around):

new_string = your_string.replace(";", ",")
new_list = new_string.split(",")

Or, a shorter version:

new_list = your_string.replace(";", ",").split(",")

In your code, this should work:

tableau_indic_final = pd.concat( [tableau_indic['code_Mono'], tableau_indic['Nom_medicament'], tableau_indic['Indications'].str.replace(';', ',').split(',', expand=True).add_prefix('Indication')], axis=1 )
Ayush Garg
  • 2,234
  • 2
  • 12
  • 28