Search code examples
pythonpandasdataframecomplex-numbers

How do I customize the display format of a DataFrame with complex numbers?


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?


Solution

  • 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