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.
JavaScript
x
13
13
1
my_list
2
[0.0,
3
0.0,
4
0.039403929840504,
5
0.000020304030304,
6
0.0,
7
0.00010000000000331966,
8
0.0,
9
0.0,
10
9.99999999891088e-05,
11
0.00010000000000331966,
12
0.0]
13
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
JavaScript
1
34
34
1
In [18]: my_list
2
Out[18]:
3
[0.0,
4
0.0,
5
0.039403929840504,
6
2.0304030304e-05,
7
0.0,
8
0.00010000000000331966,
9
0.0,
10
0.0,
11
9.99999999891088e-05,
12
0.00010000000000331966,
13
0.0]
14
15
In [19]: df = pd.DataFrame(my_list)
16
17
In [20]: df
18
Out[20]:
19
0
20
0 0.000000
21
1 0.000000
22
2 0.039404
23
3 0.000020
24
4 0.000000
25
5 0.000100
26
6 0.000000
27
7 0.000000
28
8 0.000100
29
9 0.000100
30
10 0.000000
31
32
In [21]: df.loc[3][0]
33
Out[21]: 2.0304030304e-05
34