Reputation: 26169
How can I explicitly access the excellent Series string formatting built-in to Pandas, to format my numbers like so:
>>> pd.Series([0.000034, 0.000035, 0.000038])
0 0.000034
1 0.000035
2 0.000038
dtype: float64
The astype
only results in Python's plain float-to-string conversion:
>>> pd.Series([0.000034, 0.000035, 0.000038]).astype(str)
0 3.4e-05
1 3.5e-05
2 3.8e-05
dtype: object
(I realize I could parse the string generated by pd.Series.__str__
, but looking for something less hackish.)
Edit: the reason I'd like to use the built-in Pandas formatting is that I don't know the range of the floats in advance.
Upvotes: 0
Views: 126
Reputation: 879701
Given a sequence of floats, to get a Series of strings you could use:
import pandas as pd
import pandas.io.formats.format as pf
s = pd.Series(pf.FloatArrayFormatter([0.000034, 0.000035, 0.000038])
.get_result_as_array())
print(s)
which prints
0 0.000034
1 0.000035
2 0.000038
dtype: object
Upvotes: 3
Reputation: 76927
Use format
In [1149]: pd.Series([0.000034, 0.000035, 0.000038]).apply('{:.6f}'.format)
Out[1149]:
0 0.000034
1 0.000035
2 0.000038
dtype: object
Upvotes: 3