I am trying to have a comma thousands separator for easier viewing in several columns of my pandas df.
My question builds upon this question+answer. But I want to apply it to several columns and I fail.
A minimal example:
# A random df
df = pd.DataFrame({'a' : np.random.normal(200000, 10000, 6),
'c' : np.random.normal(200000, 100000, 6)})
# This works:
df['a'] = df.a.apply(lambda x : "{:,}".format(x))
# But this doesn't
df = df.apply(lambda x : "{:,}".format(x) if x.name in ['a', 'c'] else x)
Would greatly appreciate if somebody could show me the way. Note that I want to apply this function to several dozen columns, so manually repeating the code for every column is not the solution.