1

If I have a DataFrame of floats, I can customize the display using pd.options.display.float_format, as for example pointed out in this answer. For example:

import pandas as pd
import numpy as np
pd.options.display.float_format = '{:,.2f}'.format
pd.DataFrame(np.random.randn(2, 2))

Which will give me the expected result:

enter image description here

On the other hand, if I try the same when the DataFrame contains complex floats, this doesn't work:

import pandas as pd
import numpy as np
pd.options.display.float_format = '{:,.2f}'.format
pd.DataFrame(np.random.randn(2, 2) + 1j * np.random.randn(2, 2))

produces:

enter image description here

I don't see any specific mention of complex numbers in the relevant docs, and '{:,.2f}'.format(np.complex(1.12345 + 1j * .8746234)) correctly truncates real and imaginary part of the complex number, so why doesn't this work?

glS
  • 1,209
  • 6
  • 18
  • 45
  • the datatype for complex number is `numpy.complex128` - maybe `pd.options.display.float_format` does not apply to this type – Evgeny Jun 15 '18 at 13:29
  • You may just have to use applymap to get the desired formatting. – will7200 Jun 15 '18 at 14:42
  • @will7200 could you expand on that? Does that require to change the data in the dataframe? An example would be nice – glS Jun 15 '18 at 14:45

3 Answers3

3

I am new to python but, I found this and I like the output

import pandas as pd

pd.set_option('display.float_format', '{:,.0f}'.format)

In this option you can use the pd.set_option in order to get your desired output

theletz
  • 1,713
  • 2
  • 16
  • 22
1

This is one way of obtaining the desired output using applymap, it will apply cell wise.

pd.DataFrame(np.random.randn(2, 2) + 1j * np.random.randn(2, 2)).applymap(lambda x: '{:,.2f}'.format(x))

Output:

            0            1
0  1.10-1.15j   0.49+0.65j
1  0.51+1.39j  -0.92-0.07j
will7200
  • 1,496
  • 1
  • 13
  • 23
0

display.float_format does not work for complex numbers.

display.precision work for both floats and complex values.

e.g.: pd.set_option("display.precision", 3))

wilkben
  • 657
  • 3
  • 12