简体   繁体   中英

How can I write a function for empty cell in CSV file in Python?

I am writing some programming using CSV file in Python. It will have some empty cells also. So when it reads empty cells, it should pass that cell and print the next cell. I have written this code:

number = 1
while number < 50:
    if data.D2[number] == "nan":
        pass
    else:
        print (data.D2[number])
    number = number + 1

and getting this output:

nan nan nan 2.0 2.0 2.0

The nan value it's showing in output is actually a blank cell. I am sure I am giving the wrong value of an empty cell. Does anyone know what value to write for an empty cell to pass it?

Thanks in advance!

Replace

if data.D2[number] == "nan":

with:

if data.D2[number] == "":

As I was not able to find anything which could detect the null value and pass, I replaced it with some other value

data.fillna("xyz", inplace=True)

and passed that value. And it worked fine!!

number = 1
while number < 50:
    if data.D2[number] == "xyz":
        pass
    else:
        print (data.D2[number])
    number = number + 1

I appreciate it if someone has a better solution. Meantime, I can use this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM