Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

excel - Python Pandas read_excel doesn't recognize null cell

My excel sheet:

   A   B  
1 first second
2
3 
4  x   y  
5  z   j

Python code:

df = pd.read_excel (filename, parse_cols=1)

return a correct output:

  first second
0 NaN   NaN
1 NaN   NaN
2 x     y
3 z     j

If i want work only with second column

df = pd.read_excel (filename, parse_cols=[1])

return:

 second
0  y
1  j

I'd have information about empty excel rows (NaN in my df) even if I work only with a specific column. If output loose NaN information it's not ok, for example, for skiprows paramater, etc

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For me works parameter skip_blank_lines=False:

df = pd.read_excel ('test.xlsx', 
                     parse_cols=1, 
                     skip_blank_lines=False)
print (df)

       A       B
0  first  second
1    NaN     NaN
2    NaN     NaN
3      x       y
4      z       j

Or if need omit first row:

df = pd.read_excel ('test.xlsx', 
                     parse_cols=1, 
                     skiprows=1,
                     skip_blank_lines=False)
print (df)

  first second
0   NaN    NaN
1   NaN    NaN
2     x      y
3     z      j

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...