I've been using the same thing over and over again, so make a note of it. It is used when storing data in pandas and saving it as to_csv in a file. Use this when you want to have a clean format for publishing, and when you want to have the same number of digits.
--To specify the whole float, the argument of to_csv can be set as float_format ="% 10.4f "
--You can use round ()
.
--You can take an integer as an argument to Series and round it with a decimal point or the number of digits of the integer (-2 for the hundreds digit).
--When using round
for DataFrame, you can set independently for multiple columns by taking a dictionary as an argument.
--To specify the format for each column, you can specify it with map (lambda x:'{format}'.format (x)). You can use {0: .1f}
for one digit less than a few points per week, and {: .6e}
for exponential notation.
import pandas as pd
df = pd.read_csv("latlon.csv")
df['val'] = df['val'].map(lambda x: '{0:.1f}'.format(x))
df.to_csv("out.csv", index=False, header=True, float_format='%11.6f')
And out.csv is
year,month,day,lat,lon,val
2012,6,16,80.862745,-39.834254,0.0
2012,6,16,80.862745,-39.502762,0.1
Is output as. The same content uses round
to
df = df.round({"lat":7, "lon":7, "val":1})
Recommended Posts