2016-12-15 169 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)))