Remove List If Nan Is In A Lists Of Lists
Solution 1:
Removing values from list
Using filter
nan
isnt proper Python syntax and is only used in the Pandas
library. So I am going to replace this value with None
.
we can use a filter
function to remove any values within the list that contains a nan
value.
test = list(filter(lambda x: Nonenotin x, test))
Using list comprehension
Edit: Should be x
instead of test
. Thanks @fountainhead
test = [x for x in test if None not in test]
test = [x for x intestif None not in x]
Both will return a list with values not containing None
in them
Plotting
We can use a library called matplotlib
to plot a graph. For example, to create a scatter plot:
import matplotlib.pyplotas plt
plt.scatter(x, y)
where plt.scatter takes in x values and y values for row and columns respectively. The full code is as follows:
import matplotlib.pyplot as plt
test = [[1,2],[1,None],[3,4]]
test = list(filter(lambda x: Nonenotin x, test))
x = [x for x,y in test]
y = [y for x,y in test]
# Assume allfirst elements are x valuessecond elements are y values
plt.scatter(x, y)
plt.show()
The example images is as follows
Solution 2:
You could re-construct your list using list comprehension, without the sub-lists having nans:
importmathtest= [sub_list for sub_list in test if not any(math.isnan(x)for x in sub_list)]
If you want to delete from the test
list 'in-place', you could do this:
import math
for (which, sub_list) inenumerate(test[::-1]):
ifany(math.isnan(x) for x in sub_list):
del test[len(test)-1-which]
Post a Comment for "Remove List If Nan Is In A Lists Of Lists"