2021年3月24日星期三

Easy way to convert str to float, round, then convert to int with Pandas?

I have a column in my dataframe like this:

   Average  0    235.4  1    354.7  2    508.6  

The values are strings. I want to round them to the closest integer.

Is there any easier way to achieve this than this:

df["Average"] = df["Average"].astype(float)  df["Average"] = df["Average"].round()  df["Average"] = df["Average"].astype(int)  

Thank you!

https://stackoverflow.com/questions/66791991/easy-way-to-convert-str-to-float-round-then-convert-to-int-with-pandas March 25, 2021 at 09:57AM

没有评论:

发表评论