Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

Pandas Python: one-liner to print all relevant columns with comma thousands separator?

Suppose I have a Pandas DataFrame like:

    m1       m2
0   24899    24899
1   15       1

Is there an easy one-liner that allows automatic formatting with a comma thousands separator? Looking for:

    m1       m2
0   24,899   24,899
1   15       1

I've seen multi-line solutions or solutions that use lambda functions applied to Series objects that have to be manually specified. Ideally, I'm looking for a dynamic, general solution that would work for a DataFrame with mixed dtypes. Worst case, a solution for the toy example above without having to specify each variable name in my DataFrame.

question from:https://stackoverflow.com/questions/65931334/pandas-python-one-liner-to-print-all-relevant-columns-with-comma-thousands-sepa

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about this

df['m1'] = df['m1'].map('{:,}'.format)

df['m2'] = df['m2'].map('{:,}'.format)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...