Pandas python熊猫:如何在latex中以十的幂格式化大数字

Pandas python熊猫:如何在latex中以十的幂格式化大数字,pandas,latex,Pandas,Latex,我正在使用pandas生成一些大/小数字的大型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^{

我正在使用pandas生成一些大/小数字的大型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
??定义

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)))
您可以将该函数应用于数据帧(并应用于系列)

这已经在jupyter笔记本中提供了tex输出。
注意,在这里逃跑可能很棘手。这里还有其他更快的解决方案吗?

谢谢@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)))
#定义科学记数法字符串格式的函数
def exp_tex(浮点数):
"""
返回科学表达式的字符串表示形式
给定数字的表示法,格式化后用于
LaTeX或Mathtext。
"""
负=假
如果浮点数==0.0:
返回r“$0.0”
elif浮点数<0.0:
负=真
指数=np.底板(np.log10(abs(浮点数)))
尾数=浮点数/10**指数
如无:
尾数=-尾数
尾数格式=str(尾数)[0:3]
返回“${0}\\times10^{{{1}}$”\
.format(尾数格式,str(整数(指数)))

我认为最好不要在这里重新发明轮子,而是使用python提供的浮点格式工具:

def float_exponent_notation(float_number, precision_digits, format_type="g"):
    """
    Returns a string representation of the scientific
    notation of the given number formatted for use with
    LaTeX or Mathtext, with `precision_digits` digits of
    mantissa precision, printing a normal decimal if an
    exponent isn't necessary.
    """
    e_float = "{0:.{1:d}{}}".format(float_number, precision_digits, format_type)
    if "e" not in e_float:
        return "${}$".format(e_float)
    mantissa, exponent = e_float.split("e")
    cleaned_exponent = exponent.strip("+")
    return "${0} \\times 10^{{{1}}}$".format(mantissa, cleaned_exponent)
这个版本对于零、负片等都是安全的。它可以正确地进行舍入(其他版本都不会这样!)。它还提供了使用
“g”
格式代码的选项,如果小数点在您设置的精度范围内,该代码将使用十进制显示一个数字


此外,如果还传递了
escape=False
,则可以将这些作为格式化程序传递给
.to
方法。我有一个格式化程序包装器,它可以在遇到字符串时进行类型检查和转义。

看起来不错。。。我想我只需要添加一个标志来捕捉负数。。。然后处理log10,那么我应该很好。嗯,删除了它,因为
to_latex()
没有正确处理转义。不过,很抱歉删除了它。也许这也是由于我的机器,如果neg将负面转化为正面,那么你就是
,而不是正面的发展!
def float_exponent_notation(float_number, precision_digits, format_type="g"):
    """
    Returns a string representation of the scientific
    notation of the given number formatted for use with
    LaTeX or Mathtext, with `precision_digits` digits of
    mantissa precision, printing a normal decimal if an
    exponent isn't necessary.
    """
    e_float = "{0:.{1:d}{}}".format(float_number, precision_digits, format_type)
    if "e" not in e_float:
        return "${}$".format(e_float)
    mantissa, exponent = e_float.split("e")
    cleaned_exponent = exponent.strip("+")
    return "${0} \\times 10^{{{1}}}$".format(mantissa, cleaned_exponent)