Reputation: 3897
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
Reputation: 294298
Use applymap
df = pd.DataFrame(np.random.randint(1000, 10000, (5, 5)))
df.applymap('{:,}'.format)
Upvotes: 4