I am trying to create a data series containing strings of the form:
"%.2f +/- %.2f"
using two pandas data series (the measurements and their error). Here is how I've implemented this:
df["F"] = df["Fint"].map(str) + " +/- " + df["Fint Err"].map(str)
However, the map(str) removes trailing zeros from the values in the series.
Eg for source 'VLA 3 at 6lambda'.
In[101]: df["Fint"]
Out[101]:
Source Lambda Fint
VLA 2 6.0 0.15
3.6 0.19
VLA 3 6.0 0.40
3.6 0.29
In[102]: df["Fint"].map(str)
Out[102]:
Source Lambda
VLA 2 6.0 0.15
3.6 0.19
VLA 3 6.0 0.4
How do I stop the trailing zeros from being removed?
Obviously as you can see I'm actually working with a data frame (but as map is a data series func I thought this would simplify the question), if there's a way of achieving this with applymap instead I'd be happy to know.
Thanks!
If the data starts off as floats then it doesn't have "trailing zeros". But from your example it sounds like what you really want is to have all the numbers be at least 4 digits long, adding trailing zeros if need be. Try this:
# function that maps float->str, then pads with "0" up to length 4
to_4digit_str = lambda flt: str(flt).ljust(4,"0")
df["F"] = df["Fint"].map(str) + " +/- " + df["Fint Err"].map(to_4digit_str)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.