Index de l'article

Split column and get a element

Here, splitting with a comma (str.split(',')) and get the first element (.str[0]):

df['element'] = df['Myfield'].str.split(',').str[0]

Split column without to know how many column will result

df = pd.concat([
df['id'],
df['Field1'],
df['Field1'].str.split(';', expand=True).add_prefix('Field1_'),
df['Field2'],
df['Field2'].str.split(';', expand=True).add_prefix('Field2_')
], axis=1 )

 

Liens ou pièces jointes
Télécharger ce fichier (France-Departements-Deformation.zip)France-Departements-Deformation.zip[France-Departements-Deformation]335 Ko
Télécharger ce fichier (simple_countries.zip)simple_countries.zip[simple_countries]1880 Ko