I have column vectors I want to display in a table and decided to use pandas.DataFrame for that. The elements in the vectors have the scientific notation and I don't want the notation to change. Here are the vectors:
err=np.array([[0.5671],[0.4328],[0.4555e-01],[0.3305e-02], [0.2707e-04], [0.1660e-7]])
a= (1 np.sqrt(5))/2
ratio1=np.array(abs(err[1:6]/err[0:5]))
ratio2=np.array(abs(err[1:6]/err[0:5]**a))
ratio3=np.array(abs(err[1:6]/err[0:5]**2))
I made the following matrix:
table=np.hstack((ratio1, ratio2, ratio3))
[[7.63181097e-01 1.08361327e 00 1.34576106e 00]
[1.05244917e-01 1.76598890e-01 2.43172174e-01]
[7.25576290e-02 4.89533998e-01 1.59292270e 00]
[8.19062027e-03 2.79610288e-01 2.47825122e 00]
[6.13224972e-04 4.07847058e-01 2.26533052e 01]]
At this point the notation is how I want it, but when I print it using pandas.DataFrame I lose the scientific notation and I don't understand why.
pandas.DataFrame(table, columns=["ratio 1","ratio 2", "ratio 3"])
ratio 1 ratio 2 ratio 3
0 0.763181 1.083613 1.345761
1 0.105245 0.176599 0.243172
2 0.072558 0.489534 1.592923
3 0.008191 0.279610 2.478251
4 0.000613 0.407847 22.653305
I also got the exact same result when using this method:
table={}
table["ratio1"]=ratio1[:,0].tolist()
table["ratio2"]=ratio2[:,0].tolist()
table["ratio3"]=ratio3[:,0].tolist()
print(pandas.DataFrame(table, index=[1,2,3,4,5]))
Does anyone know what is causing this?
CodePudding user response:
You can use pd.set_option() before creating the dataframe to set the formatting to your needs.
err=np.array([[0.5671],[0.4328],[0.4555e-01],[0.3305e-02], [0.2707e-04], [0.1660e-7]])
a= (1 np.sqrt(5))/2
ratio1=np.array(abs(err[1:6]/err[0:5]), dtype="float")
ratio2=np.array(abs(err[1:6]/err[0:5]**a), dtype="float")
ratio3=np.array(abs(err[1:6]/err[0:5]**2), dtype="float")
table=np.hstack((ratio1, ratio2, ratio3))
pd.set_option('display.float_format','{:.8e}'.format)
df = pd.DataFrame(table, columns=["ratio 1","ratio 2", "ratio 3"])
Output:
ratio 1 ratio 2 ratio 3
0 7.63181097e-01 1.08361327e 00 1.34576106e 00
1 1.05244917e-01 1.76598890e-01 2.43172174e-01
2 7.25576290e-02 4.89533998e-01 1.59292270e 00
3 8.19062027e-03 2.79610288e-01 2.47825122e 00
4 6.13224972e-04 4.07847058e-01 2.26533052e 01
Extra note: this will also influence other dataframes that might follow in your code. If you want to reset the scientific notation you can use the following code for the float_format: pd.reset_option('display.float_format')
