Format Dataframe

I have a dataframe with columns of numbers and strings. I would like to format it like an Excel spreadsheet so the numbers have dollar signs, commas, and 2 decimals.

df = DataFrame(
StringColumn = [“A”, “B”, “C”, “D”, “E”],
FloatColumn = [112345.23, 1214.56, 7.899, 0.12, 3.45]
)

I’ve tried @sprintf(“%.2f”, df.FloatColumn), but it doesn’t like getting a vector.
Trying to broadcase @sprintf.(“%.2f”, df.FloatColumn) is not valid either.

I know we don’t usually use Julia when a pretty format is needed, but in this case I need to.

Any suggestions? Use a call to C?

Check out

https://ronisbr.github.io/PrettyTables.jl/stable/

1 Like

Another suggestion, using Formatting.jl:

using DataFrames, Formatting
df = DataFrame(x=["A","B","C","D","E"], y=[112345.23, 1214.56, 7.899, 0.12, 3.45])
DataFrame(x=df.x, y=sprintf1.("\$%'.2f", df.y))

 Row │ x       y
     │ String  String
─────┼─────────────────────
   1 │ A       $112,345.23
   2 │ B       $1,214.56
   3 │ C       $7.90
   4 │ D       $0.12
   5 │ E       $3.45
1 Like

This is also a good solution, but I can check only one reply as a solution. Thanks.

1 Like