SloppyPenguin
SloppyPenguin

Reputation: 75

Split a pandas column into two based on a delimiter that may not exist on all values

One of the columns of my dataframe looks something like this:

[application]
blah/3.14
xyz/5.2
abc
...
...

(representing software/version)

I'm trying to achieve something like this:

[application] [name]  [ver]
blah/3.14      blah    3.14
xyz/5.2        xyz     5.2 
abc            abc     na   <-- this missing value can be filled in with a string too
...  
...

As you can already tell, I'd like to split the column into two, using '/' as a delimiter. A stack overflow solution suggests something like this:

tmptbl = pd.DataFrame(main_tbl.application.str.split('/', 1).tolist(), columns= ['name', 'ver'])
main_tbl['name'] = tmptbl.name
main_tbl['ver'] = tmptbl.ver

Which looks great at first, but it crashes for columns without '/', such as 'abc'.

What else can I try?

Upvotes: 6

Views: 3169

Answers (1)

jezrael
jezrael

Reputation: 862671

Use str.split with parameter expand=True for return DataFrame:

main_tbl[['name','ver']] = main_tbl.application.str.split('/', expand=True)
print (main_tbl)
  application  name   ver
0   blah/3.14  blah  3.14
1     xyz/5.2   xyz   5.2
2         abc   abc  None

And if need NaNs add replace:

main_tbl.ver = main_tbl.ver.replace({None:np.nan})
print (main_tbl)
  application  name   ver
0   blah/3.14  blah  3.14
1     xyz/5.2   xyz   5.2
2         abc   abc   NaN

Upvotes: 4

Related Questions