Skip to content
Advertisement

How to standardize column in pandas

I have dataframe which contains id column with the following sample values

JavaScript

I want to standardise to XXXXXXXX-XXXX (i.e. 8 and 4 digits separated by a dash), How can I achieve that using python.

here’s my code

JavaScript

Advertisement

Answer

Can use DataFrame.replace() function using a regular expression like this:

JavaScript

Here’s example code with sample data.

JavaScript

Output:

JavaScript

If any value does not match the regexp of the expected format then it’s value will not be changed.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement