Python Pandas read_excel无法识别空单元格

发布于 2021-01-29 14:56:08

我的Excel工作表:

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

Python代码:

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

返回正确的输出:

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

如果我只想与第二栏

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

返回:

 second
0  y
1  j

即使我仅使用特定的列,我也将获得有关空Excel行(在df中为NaN)的信息。如果输出松散的NaN信息,则不行,例如,对于飞檐参数等

谢谢

关注者
0
被浏览
152
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    对我来说,工作参数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
    

    或者,如果需要省略第一行:

    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
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看