我有要在表格中顯示的列向量,并決定為此使用 pandas.DataFrame。向量中的元素具有科學記數法,我不希望符號改變。以下是向量:
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))
我做了以下矩陣:
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]]
此時符號是我想要的,但是當我使用 pandas.DataFrame 列印它時,我失去了科學記數法,我不明白為什么。
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
使用此方法時,我也得到了完全相同的結果:
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]))
有誰知道是什么原因造成的?
uj5u.com熱心網友回復:
您可以在創建資料框之前使用 pd.set_option() 來根據需要設定格式。
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"])
輸出:
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
額外說明:這也會影響您的代碼中可能遵循的其他資料幀。如果要重置科學記數法,可以使用以下代碼作為 float_format:pd.reset_option('display.float_format')
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/420089.html
標籤:
