【问题标题】:Python Pandas read_excel doesn't recognize null cellPython Pandas read_excel 无法识别空单元格
【发布时间】:2017-01-13 01:22:57
【问题描述】:

我的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 信息是不行的,例如,skirows 参数等

谢谢

【问题讨论】:

标签: python excel pandas nan


【解决方案1】:

我的作品参数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

【讨论】:

  • 是的,它有效。但是为什么我在官方文档中没有看到这个参数呢? pandas.pydata.org/pandas-docs/stable/generated/…
  • General Parsing Configurationdocs,或者这个参数在read_csv
  • 在官方文档中 - read_excel 后面是 **kwds ;(
  • 我尝试省略它df = pd.read_excel ('test.xlsx', parse_cols=1, skiprows=1) 并且使用我的测试数据它也可以工作 - 我使用熊猫0.18.1
  • 在 pandas 1.1.4 中:TypeError: read_excel() got an unexpected keyword argument 'skip_blank_lines' 并且在上面发布的文档中没有提及。
猜你喜欢
  • 2018-05-14
  • 2021-03-30
  • 2017-06-10
  • 2022-07-21
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多