Split a Column Based on first instance

41 Views Asked by At

Looking to split a df | series column into 2 parts based on the first "_"

example in column:

Male_85__and_over

test['gender'] = test['column_Name_pivoted'].str.split('_').str[0]
test['age'] = test['column_Name_pivoted'].str.split('_',n=1).str[1:]

Output is not what I was looking for:

gender age
Male [85__and_over]
3

There are 3 best solutions below

2
Tim Biegeleisen On

You could use str.extract here:

test[["gender", "age"]] = test.str.extract(r'([^_]+)_([^_]+)')
0
Tinkinc On
test[['gender','age']] =  test["column_Name_pivoted"].str.split("_", n=1, expand=True)
0
brunns On

Try str.partition():

test['gender'], _, test['age'] = test['column_Name_pivoted'].str.partition("_")