uharsha33
uharsha33

Reputation: 225

Code for extracting all rows pretaining to a specific value in one column

I'm new to data science and trying to do some data wrangling with python 2.7 in iPython notebook. A tutorial I was following for my first project asked me to replace all NaN intputs with Y or N. But I'd like to consider another approach where I can 1st look at all the rows with NaN inputs for a specific column so that I can utilize the fillna() better.

Is there a code that lets me extract such rows?

I have 13 rows (loan_id, gender, married, credit_history, etc.) Most of the rows do not have NaN values and my interest is in credit_history. How do I extract all rows have NaN values under credit history?

I'd like the output to be something similar to:

loan_id gender married credit_history loan_status
1         M      Y          NaN           Y
2         F      Y          NaN           N
3         M      Y          NaN           Y
4         M      Y          NaN           N
5         F      N          NaN           Y

Upvotes: 0

Views: 53

Answers (2)

jpp
jpp

Reputation: 164793

This is one way:

df2 = df[pd.isnull(df['credit_history'])]

Explanation

  • pd.isnull creates a Boolean series in line with whether each entry in a series is NaN.
  • You can then use this as an indexer for df.

Upvotes: 0

HimanshuGahlot
HimanshuGahlot

Reputation: 571

There you go

df_null = df[df["credit_history"].isnull()]

If this doesn't solve your problem then let me know.

Upvotes: 1

Related Questions