splinter
splinter

Reputation: 3897

Applying comma seperator formatting to an entire DataFrame in python 3

I want to format all numbers in a DataFrame to have comma seperators (e.g. 1,000,000 instead of 1000000). It can be applied to a single number using '{:,}'.format(number). Naively applying the same to a DataFrame gives the error

    TypeError: non-empty format string passed to object.__format__

Is there a way to efficiently apply the same formatting to a DataFrame? Thanks!

Upvotes: 2

Views: 445

Answers (1)

piRSquared
piRSquared

Reputation: 294298

Use applymap

df = pd.DataFrame(np.random.randint(1000, 10000, (5, 5)))

df.applymap('{:,}'.format)

enter image description here

Upvotes: 4

Related Questions