2016-12-15 132 views
1

我用熊貓來產生與大/小數字大一些LaTex的表格式十的權力大的數字:蟒蛇大熊貓:如何在乳膠

df = pd.DataFrame(np.array(outfile),columns=['Halo','$r_{v}$','etc']) 
df.to_latex("uvFlux_table_{:.1f}.tex".format(z)) 

其中「OUTFILE」只是一個表數(3列)......我怎樣才能在OUTFILE數字被格式化,如:

$1.5x10^{12}$ & $1.5x10^{12}$ & $1.5x10^{-12}$ 

- 就像你在科學出版物看看...... VS默認

0.15e13 & 0.15e13 & 0.15e-11 

??

回答

1

定義

def format_tex(float_number): 
    exponent = np.floor(np.log10(float_number)) 
    mantissa = float_number/10**exponent 
    mantissa_format = str(mantissa)[0:3] 
    return "${0}\times10^{{{1}}}$"\ 
      .format(mantissa_format, str(int(exponent))) 

可以applymap在數據框該功能(並應用了一系列)

df = pd.DataFrame({'col':[12345.1200000,100000000]}) 
df.applymap(lambda x:format_tex(x)) 

這已經給出了jupyter筆記本TEX輸出。 請注意,轉義在這裏可能會非常棘手。其他更快的解決方案?

+0

這看起來不錯...我想我只需要添加一個標誌來捕捉負數...並處理日誌10,那麼我應該不錯。 – earnric

+0

嗯,刪除它,因爲'to_latex()'沒有正確處理轉義。儘管如此,刪除。也許這也是由於我的機器 – Quickbeam2k1

1

感謝@ Quickbeam2k1的答案。我已經擴展到處理0和負數:

# Define function for string formatting of scientific notation 
def exp_tex(float_number): 
    """ 
    Returns a string representation of the scientific 
    notation of the given number formatted for use with 
    LaTeX or Mathtext. 
    """ 
    neg = False 
    if float_number == 0.0: 
     return r"$0.0" 
    elif float_number < 0.0: 
     neg = True 

    exponent = np.floor(np.log10(abs(float_number))) 
    mantissa = float_number/10**exponent 
    if neg: 
     mantissa = -mantissa 
    mantissa_format = str(mantissa)[0:3] 
    return "${0}\\times10^{{{1}}}$"\ 
      .format(mantissa_format, str(int(exponent)))