The Python Oracle

How to find blank cells in excel using pandas?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC C Schuberts Piano Sonata No 13 D

--

Chapters
00:00 Question
01:27 Accepted answer (Score 1)
02:20 Answer 2 (Score 4)
02:46 Answer 3 (Score 3)
03:02 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'