The Python Oracle

How to find blank cells in excel using pandas?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Future Grid Looping

--

Chapters
00:00 How To Find Blank Cells In Excel Using Pandas?
01:02 Answer 1 Score 4
01:22 Accepted Answer Score 1
02:03 Answer 3 Score 3
02:13 Thank you

--

Full question
https://stackoverflow.com/questions/5735...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #excel #pandas

#avk47



ANSWER 1

Score 4


if your cell in excel is totally empty then print the Array variable will output a 'nan'. so if it contains nan then check:

import pandas as pd

if pd.isnull(Array):

if the above one is not working then there might be spaces, so try:

if Array.strip() == "":



ANSWER 2

Score 3


Try using isnan() function from numpy library

import numpy as np

for Array in LastName:
    if np.isnan(Array):
        print('Cell is empty')



ACCEPTED ANSWER

Score 1


Based on the comments, OP wants to drop rows if there's empty cell (i.e. NaN in pandas).

Using modified version from OP's sample data (Note I added last row where column A has empty cell, too):

print(df)
       A          B
0  Name1  LastName1
1  Name2        
2  Name3  LastName3
3  Name4  LastName4
4         LastName5

Using df.dropna() will drop any row if any corresponding column has empty cell:

       A          B
0  Name1  LastName1
2  Name3  LastName3 <<< row 1 is dropped 
3  Name4  LastName4 <<< row 4 is dropped

Using df.dropna(subset=['B']) will drop only if column B has empty cell:

       A          B
0  Name1  LastName1
2  Name3  LastName3 <<< row 1 is dropped since (2, 'B') was empty
3  Name4  LastName4
4    NaN  LastName5 <<< row 4 is not dropped since dropna is only looking at 'B'