Skip to content
Advertisement

I lose decimals when adding a list of floats to a dataframe as a column

I have a list of floats that I would like to add to a dataframe as a new column. Each float has aproximately 9 o 10 decimals and I need all of them. The problem is that when I add the list to the dataframe as a new column I lose the decimals (only four of them remain). The methods I used for adding the list were first pd.concat and then just creating a new variable in the dataframe that is exactly the same as the list, but none of them worked.

my_list
[0.0,
 0.0,
 0.039403929840504,
 0.000020304030304,
 0.0,
 0.00010000000000331966,
 0.0,
 0.0,
 9.99999999891088e-05,
 0.00010000000000331966,
 0.0]

Thats the list and I would like to add it to a dataframe as a new column without losing any decimal points. By the way, the dataframe has the same number of rows that elements in the list.

Advertisement

Answer

You don’t lose precision, it’s just not printed out

In [18]: my_list
Out[18]:
[0.0,
 0.0,
 0.039403929840504,
 2.0304030304e-05,
 0.0,
 0.00010000000000331966,
 0.0,
 0.0,
 9.99999999891088e-05,
 0.00010000000000331966,
 0.0]

In [19]: df = pd.DataFrame(my_list)

In [20]: df
Out[20]:
           0
0   0.000000
1   0.000000
2   0.039404
3   0.000020
4   0.000000
5   0.000100
6   0.000000
7   0.000000
8   0.000100
9   0.000100
10  0.000000

In [21]: df.loc[3][0]
Out[21]: 2.0304030304e-05
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement