index of non NaN values in Pandas

2024/11/19 11:23:58

From Pandas data frame, how to get index of non "NaN" values?

My data frame is

    A    b     c
0   1    q1    1
1   2    NaN   3
2   3    q2    3
3   4    q1    NaN
4   5    q2    7

And I want the index of the rows in which column b is not NaN. (there can be NaN values in other column e.g. c )

non_nana_index = [0,2,3,4]

Using this non "NaN" index list I want to create new data frame which column b do not have "Nan"

df2=

    A    b     c
0   1    q1    1
1   3    q2    3
2   4    q1    NaN
3   5    q2    7
Answer

Just filter them

In [62]:df['b'].notnull()Out[62]:
0     True
1    False
2     True
3     True
4     True
Name: b, dtype: bool
In [63]:df[df['b'].notnull()]
Out[63]:A   b   c
0  1  q1   1
2  3  q2   3
3  4  q1 NaN
4  5  q2   7
https://en.xdnf.cn/q/26442.html

Related Q&A

How can I type-check variables in Python? [duplicate]

This question already has answers here:Whats the canonical way to check for type in Python?(16 answers)Closed 3 months ago.I have a Python function that takes a numeric argument that must be an intege…

Py_INCREF/DECREF: When

Is one correct in stating the following:If a Python object is created in a C function, but the function doesnt return it, no INCREF is needed, but a DECREF is. [false]If the function does return it, yo…

pop/remove items out of a python tuple

I am not sure if I can make myself clear but will try.I have a tuple in python which I go through as follows (see code below). While going through it, I maintain a counter (lets call it n) and pop item…

Difference between frompyfunc and vectorize in numpy

What is the difference between vectorize and frompyfunc in numpy?Both seem very similar. What is a typical use case for each of them?Edit: As JoshAdel indicates, the class vectorize seems to be built…

Jupyter notebook command does not work on Mac

I installed jupyter using pip on my macbook air. Upon trying to execute the command jupyter notebook, I get an error jupyter: notebook is not a Jupyter commandI used the --h option to get a listing of …

Recursively compare two directories to ensure they have the same files and subdirectories

From what I observe filecmp.dircmp is recursive, but inadequate for my needs, at least in py2. I want to compare two directories and all their contained files. Does this exist, or do I need to build …

Specific reasons to favor pip vs. conda when installing Python packages

I use miniconda as my default python installation. What is the current (2019) wisdom regarding when to install something with conda vs. pip?My usual behavior is to install everything with pip, and onl…

Insert a link inside a Pandas table

Id like to insert a link (to a web page) inside a Pandas table, so when it is displayed in an IPython notebook, I could press the link. I tried the following: In [1]: import pandas as pdIn [2]: df = pd…

TypeError: string indices must be integers while parsing JSON using Python?

I am confuse now why I am not able to parse this JSON string. Similar code works fine on other JSON string but not on this one - I am trying to parse JSON String and extract script from the JSON.Below …

Python dynamic inheritance: How to choose base class upon instance creation?

IntroductionI have encountered an interesting case in my programming job that requires me to implement a mechanism of dynamic class inheritance in python. What I mean when using the term "dynamic …