Skip to content
Advertisement

How format different values in pandas?

i have a columns of dataframe with 1000+ different format of values. how can i format these in order to have unified view like this 1.000.000.

JavaScript

for example:

  • row 1 is desiderated view
  • row 2 10000000 should be 10.000.000
  • row 3 150,250 should be 150.250
  • row 4 0,200655 should be 200.655

Advertisement

Answer

For your input this should work:

JavaScript

Here we:

  1. get each element in Sales column and remove ‘,’ and ‘.’ ie ‘150,250’ -> ‘150250’
  2. Then covert the string into int ie ‘150250’ -> 150250
  3. then use "{:,}".format() to format int to string with commas ie 150250 -> ‘150,250’
  4. In this converted string we replace commas ‘,’ with period ‘.’ ie ‘150,250’ -> ‘150.250’
  5. make an array with the results and assign it to Sales column of Dataframe

Input:

JavaScript

Output:

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