Skip to content
Advertisement

Why do I lose numerical precision when extracting element from list in python?

I have a pandas dataframe that looks like this:

                    data
0    [26.113017616106, 106.948066803935, 215.488217...
1    [26.369709448639, 106.961107298101, 215.558911...
2    [26.261267444521, 106.991763898421, 215.384122...
3    [26.285746968657, 106.912377030428, 215.287348...
4    [26.155342026996, 106.825440402654, 215.114619...
5    [26.159917638984, 106.819720887669, 215.117593...
6    [26.023564401739, 106.843056508808, 215.129947...
7    [26.1155342027, 106.828185769847, 215.15991763...
8    [26.028826355525, 106.841912605811, 215.146190...
9    [26.015099519561, 106.824296499657, 215.130404...

I am trying to extract the 1st element from the Series of lists using this code:

[x[1] for x in df.data]

and I get this result:

0    106.948067
1    106.961107
2    106.991764
3    106.912377
4    106.825440
5    106.819721
6    106.843057
7    106.828186
8    106.841913
9    106.824296

Why do I lose precision and what can I do to keep it?

Advertisement

Answer

By default, pandas displays floating-point values with 6 digits of precision.

You can control the precision with pandas’ set_option e.g.

pd.set_option('precision', 12)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement