Posts: 34
Threads: 10
Joined: Oct 2021
I have this dict:
Output: [438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08000000000004, nan, nan, nan, nan]
Please how can I remove the nan values?
I have used my_dict.pop(nan) but it doesn't work since nan is not a string
Posts: 1,144
Threads: 114
Joined: Sep 2019
nan = 0
mylist = [438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08000000000004, nan, nan, nan, nan]
for i in range(len(mylist)):
if i >= 6:
mylist.pop()
print(mylist) Output: [438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08000000000004]
Posts: 1,583
Threads: 3
Joined: Mar 2020
The object you show isn't a dict, but a list.
When you use list.pop(), you feed it the location of the item to be removed, not the value of the item. Removing items that will fail direct matches is a bit tricky. You could use a comprehension to make a copy of the list with just the non-nan values.
>>> l
[438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08, nan, nan, nan, nan]
>>> import math
>>> newlist = [x for x in l if not math.isnan(x)]
>>> newlist
[438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08]
Posts: 7,313
Threads: 123
Joined: Sep 2016
You should tell where the list come from(not a dict) as it has nan lowercase it most likely come from NumPy.
When use eg NumPy, Pandas the way to solve can be very different from doing it in stander Python.
nan will give a error so menator01 has solved it by using nan = 0 ,not ideal but that because of lacking information from OP as mention.
So it can be done like this.
# Give error
>>> lst = [438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08000000000004, nan, nan, nan, nan]
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
NameError: name 'nan' is not defined
>>> import numpy as np
>>> from numpy import nan # Fix error
>>>
>>> lst = [438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08000000000004, nan, nan, nan, nan]
>>> [x for x in lst if ~np.isnan(x)]
[438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08000000000004]
Posts: 6,780
Threads: 20
Joined: Feb 2020
Assuming it is a numpy array.
import numpy as np
notadict = np.array([438.4, 439.18, 439.9, np.nan, np.nan])
print(notadict[(~np.isnan(notadict))], notadict)
print(np.delete(notadict, np.isnan(notadict)), notadict)
print(np.nan_to_num(notadict, nan=0.0), notadict)
print(np.nan_to_num(notadict, copy=False, nan=0.0), notadict) Output: [438.4 439.18 439.9 ] [438.4 439.18 439.9 nan nan]
[438.4 439.18 439.9 ] [438.4 439.18 439.9 nan nan]
[438.4 439.18 439.9 0. 0. ] [438.4 439.18 439.9 nan nan]
[438.4 439.18 439.9 0. 0. ] [438.4 439.18 439.9 0. 0. ]
Notice that nan_to_num does the change in place if you use "copy=False".
Posts: 34
Threads: 10
Joined: Oct 2021
Oct-05-2021, 06:20 PM
(This post was last modified: Oct-05-2021, 06:23 PM by tomtom.)
(Oct-05-2021, 03:33 PM)menator01 Wrote: nan = 0
mylist = [438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08000000000004, nan, nan, nan, nan]
for i in range(len(mylist)):
if i >= 6:
mylist.pop()
print(mylist) Output: [438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08000000000004]
Thank you so much sir, but the len of the list is not fixed it will be determine by the bars the traded cryptocurrency
Posts: 34
Threads: 10
Joined: Oct 2021
(Oct-05-2021, 02:57 PM)tomtom Wrote: I have this dict:
Output: [438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08000000000004, nan, nan, nan, nan]
Please how can I remove the nan values?
I have used my_dict.pop(nan) but it doesn't work since nan is not a string
list not a dict
Posts: 1,144
Threads: 114
Joined: Sep 2019
(Oct-05-2021, 06:20 PM)tomtom Wrote: (Oct-05-2021, 03:33 PM)menator01 Wrote: nan = 0
mylist = [438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08000000000004, nan, nan, nan, nan]
for i in range(len(mylist)):
if i >= 6:
mylist.pop()
print(mylist) Output: [438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08000000000004]
Thank you so much sir, but the len of the list is not fixed it will be determine by the bars the traded cryptocurrency
This will not matter the list length. You should use one of the more experienced programmers suggestions though.
#! /usr/bin/env python3
nan = 0
mylist = [438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08000000000004, nan, nan, nan, nan]
for i in range(len(mylist)):
if 0 in mylist:
mylist.pop()
print(mylist)
Posts: 34
Threads: 10
Joined: Oct 2021
(Oct-05-2021, 03:39 PM)bowlofred Wrote: The object you show isn't a dict, but a list.
When you use list.pop(), you feed it the location of the item to be removed, not the value of the item. Removing items that will fail direct matches is a bit tricky. You could use a comprehension to make a copy of the list with just the non-nan values.
>>> l
[438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08, nan, nan, nan, nan]
>>> import math
>>> newlist = [x for x in l if not math.isnan(x)]
>>> newlist
[438.4, 439.18, 439.9, 440.21999999999997, 440.38, 441.08]
I used this and it worked fine, thank you sir
|