Skip to content
Advertisement

Split string and put the extracted string to another new column in python

My data –

JavaScript

So in this data, I need to split dept column, and need to create another column where my values should be only PI or DI. Is it possible to create a code where it accurately takes out only PI or DI from dept column

My output –

JavaScript

I have tried –

JavaScript

but this doesn’t serve the purpose, because ‘ – ‘ this dash is not same, somewhere it is occurring like ‘ -‘, so I have to rework on it, and also I get a few results like – PI (Supervisor) so I have to remove (Supervisor) part. The data is not same, it varies, I only need PI or DI. So I need a code in which, wherever it finds PI or DI , it will extract it and then create a new column where it only stores PI and DI only. Please help.

Advertisement

Answer

You might be able to just use str.extract here:

JavaScript

For department values not having either DI or PI in them, the value of division would be NaN.

Advertisement