Removing nan from list - Python

2024/9/25 18:19:41

I am trying to remove 'nan' from a list, but it is refusing to go. I have tried both np.nan and 'nan'.

This is my code:

ztt = []
for i in z:if i != 'nan':ztt.append(i) 
ztt

or:

ztt = []
for i in z:if i != np.nan:ztt.append(i) 
ztt

I am still getting the output:

[[46.0, 34.0, 32.0, 40.0, 34.0, 29.0, 38.0, 39.0, 45.0, 32.0, 28.0, 43.0],[32.0, 30.0, 67.0, 66.0, 28.0, 19.0, 39.0, 32.0, 51.0, 28.0, 20.0, 36.0],[29.0, 24.0, 37.0, 31.0, 32.0, 34.0, 28.0, 31.0, 28.0, 33.0, 28.0, 39.0],[27.0, 29.0, 35.0, nan, nan, nan, nan, nan, nan, nan, nan, nan]]

Anyone know what is going wrong?

Answer
for i in z:if not math.isnan(i):ztt.append(i)
https://en.xdnf.cn/q/71549.html

Related Q&A

Safely unpacking results of str.split [duplicate]

This question already has answers here:How do I reliably split a string in Python, when it may not contain the pattern, or all n elements?(5 answers)Closed 6 years ago.Ive often been frustrated by the…

Get a structure of HTML code

Im using BeautifulSoup4 and Im curious whether is there a function which returns a structure (ordered tags) of the HTML code. Here is an example:<html> <body> <h1>Simple example</h…

max_help_position is not works in python argparse library

Hi colleagues I have the code (max_help_position is 2000):formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=2000) parser = argparse.ArgumentParser(formatter_class=formatter_cl…

Python - How to parse argv on the command line using stdin/stdout?

Im new to programming. I looked at tutorials for this, but Im just getting more confused. But what Im trying to do is use stdin and stdout to take in data, pass it through arguments and print out outpu…

Bad Request from Yelp API

Inspired by this Yelp tutorial, I created a script to search for all gyms in a given city. I tweaked the script with these updates in order to return ALL gyms, not just the first 20. You can find the g…

Python: test empty set intersection without creation of new set

I often find myself wanting to test the intersection of two sets without using the result of the intersections.set1 = set([1,2]) set2 = set([2,3]) if(set1 & set2):print("Non-empty intersection…

object has no attribute show

I have installed wxpython successfully which i verified by import wxBut when I write a code import wx class gui(wx.Frame):def __init__(self,parent,id):wx.Frame.__init__(self, parent,id,Visualisation fo…

How Does Calling Work In Python? [duplicate]

This question already has answers here:Does Python make a copy of objects on assignment?(5 answers)How do I pass a variable by reference?(40 answers)Why can a function modify some arguments as percei…

Python: Sklearn.linear_model.LinearRegression working weird

I am trying to do multiple variables linear regression. But I find that the sklearn.linear_model working very weird. Heres my code:import numpy as np from sklearn import linear_modelb = np.array([3,5,7…

Implementation of Gaussian Process Regression in Python y(n_samples, n_targets)

I am working on some price data with x = day1, day2, day3,...etc. on day1, I have lets say 15 price points(y), day2, I have 30 price points(y2), and so on.When I read the documentation of Gaussian Proc…