I am trying to export a DataFrame to Latex using a formatter function to specify the format of the columns. So far I use a function which can be used to specify the format of each column. I have the following code snippet which works fine.
def use_f_2(x):
return "%.2f" % x
df.to_latex(formatters=[None, use_f_2, use_f_2])
This rounds the values in columns 2 and 3 to the second decimal place. Now I would like to extend this function to additionally keep the number of decimal places flexible (which is fixed to 2 in the above scenario). I would imagine it to be something like this
def use_f_2(x, num_decimals):
return f"%.{num_decimals}f" % x
However this function cannot be used in
df.to_latex(formatters=[None, use_f_2, use_f_2])
as there are two arguments now.
Does anyone know how to implement this?
Many thanks in advance!