How to delete numpy nan from a list of strings in Python?

2024/9/8 10:55:26

I have a list of strings

x = ['A', 'B', nan, 'D']

and want to remove the nan.

I tried:

x = x[~numpy.isnan(x)]

But that only works if it contains numbers. How do we solve this for strings in Python 3+?

Answer

If you have a numpy array you can simply check the item is not the string nan, but if you have a list you can check the identity with is and np.nan since it's a singleton object.

In [25]: x = np.array(['A', 'B', np.nan, 'D'])In [26]: x
Out[26]: 
array(['A', 'B', 'nan', 'D'], dtype='<U3')In [27]: x[x != 'nan']
Out[27]: 
array(['A', 'B', 'D'], dtype='<U3')In [28]: x = ['A', 'B', np.nan, 'D']In [30]: [i for i in x if i is not np.nan]
Out[30]: ['A', 'B', 'D']

Or as a functional approach in case you have a python list:

In [34]: from operator import is_notIn [35]: from functools import partialIn [37]: f = partial(is_not, np.nan)In [38]: x = ['A', 'B', np.nan, 'D']In [39]: list(filter(f, x))
Out[39]: ['A', 'B', 'D']
https://en.xdnf.cn/q/73052.html

Related Q&A

Pasting data into a pandas dataframe

This is the same question as this question, which is marked as a duplicate of this one. The problem, and the reason Im still asking, is that the solution provided (using pandas.read_clipboard()) curren…

Add graph description under graph in pylab [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Is there a way of drawing a caption box in matplotlib Is it possible to add graph description under graph in pylab?Lets s…

Using Python textwrap.shorten for string but with bytes width

Id like to shorten a string using textwrap.shorten or a function like it. The string can potentially have non-ASCII characters. Whats special here is that the maximal width is for the bytes encoding of…

How to create a transparent mask in opencv-python

I have sign (signs with arbitrary shape) images with white background and I want to get an image of the sign with transparent background. I have managed to create a mask and apply it to the image and t…

Variables with dynamic shape TensorFlow

I need to create a matrix in TensorFlow to store some values. The trick is the matrix has to support dynamic shape.I am trying to do the same I would do in numpy: myVar = tf.Variable(tf.zeros((x,y), va…

python protobuf cant deserialize message

Getting started with protobuf in python I face a strange issue:a simple message proto definition is:syntax = "proto3"; package test;message Message {string message = 1;string sender = 2; }gen…

Seaborn: title and subtitle placement

H all,Id like to create a scatterplot with a title, subtitle, colours corresponding to a specific variable and size corresponding to another variable. I want to display the colour legend but not the si…

Calculate a rolling regression in Pandas and store the slope

I have some time series data and I want to calculate a groupwise rolling regression of the last n days in Pandas and store the slope of that regression in a new column.I searched the older questions an…

Python read microphone

I am trying to make python grab data from my microphone, as I want to make a random generator which will use noise from it. So basically I dont want to record the sounds, but rather read it in as a da…

How to tell pytest-xdist to run tests from one folder sequencially and the rest in parallel?

Imagine that I have test/unit/... which are safe to run in parallel and test/functional/... which cannot be run in parallel yet.Is there an easy way to convince pytest to run the functional ones sequen…