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
190 views
in Technique[技术] by (71.8m points)

python - Round columns in pandas dataframe

I have got the following pandas data frame

          Y         X id WP_NER
0 35.973496 -2.734554  1  WP_01 
1 35.592138 -2.903913  2  WP_02 
2 35.329853 -3.391070  3  WP_03 
3 35.392608 -3.928513  4  WP_04 
4 35.579265 -3.942995  5  WP_05 
5 35.519728 -3.408771  6  WP_06 
6 35.759485 -3.078903 7 WP_07 

I′d like to round Y and X columns using pandas. How can I do that ?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You can now, use round on dataframe

Option 1

In [661]: df.round({'Y': 2, 'X': 2})
Out[661]:
       Y     X  id WP_NER
0  35.97 -2.73   1  WP_01
1  35.59 -2.90   2  WP_02
2  35.33 -3.39   3  WP_03
3  35.39 -3.93   4  WP_04
4  35.58 -3.94   5  WP_05
5  35.52 -3.41   6  WP_06
6  35.76 -3.08   7  WP_07

Option 2

In [662]: cols = ['Y', 'X']

In [663]: df[cols] = df[cols].round(2)

In [664]: df
Out[664]:
       Y     X  id WP_NER
0  35.97 -2.73   1  WP_01
1  35.59 -2.90   2  WP_02
2  35.33 -3.39   3  WP_03
3  35.39 -3.93   4  WP_04
4  35.58 -3.94   5  WP_05
5  35.52 -3.41   6  WP_06
6  35.76 -3.08   7  WP_07

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

...