【问题标题】:Reading in Empty Cells with `read_excel` in Python pandas在 Python pandas 中使用“read_excel”读取空单元格
【发布时间】:2018-05-14 14:13:09
【问题描述】:

我正在使用 Python 熊猫 read_excel。这是我正在阅读的专栏。

我的问题是 read_excel 没有将空单元格计为单元格。当我使用df2=df1.iloc[0:30] 时,我希望它包含那些空单元格,因此最后两个数据项不包含在我的数据框中(这是因为这些单元格在整个月中每天都会填充,所以这些空单元格将一直存在到最后月中的某天)。如何确保 pandas read_excel 在其数据框中包含那些空单元格?

【问题讨论】:

  • 尝试在读取函数中添加参数skip_blank_lines=False
  • 这成功了!除了pandas.pydata.org/pandas-docs/stable/generated/…,还有其他文档吗?我在参数列表中没有看到。
  • 据我所知,它是无证的,但在互联网上停留一段时间后你就会知道它是一个隐藏的宝石。 :$
  • 在熊猫 1.1.4 中:read_excel() got an unexpected keyword argument 'skip_blank_lines'

标签: python excel pandas dataframe


【解决方案1】:
df = pd.read_excel('book1.xlsx',header=None, skip_blank_lines=False)

       0
0     17
1      0
2      0
3      0
4      0
5      T
6   0.13
7   0.33
8   0.02
9   0.04
10     T
11     0
12     0
13  0.57
14     0
15     0
16     T
17     0
18     0
19  0.07
20     0
21     0
22  0.11
23     0
24     0
25   NaN
26   NaN
27   NaN
28   NaN
29   NaN
30   NaN
31  1.27
32     7

#注意:Count 不计算 NaN 值。

df.count()

返回

0    27
dtype: int64

df.size

返回

33

【讨论】:

  • len(df)@Scott 怎么样
  • @pyd len(df) 返回 33 它包括 NaN 以及 df.shape 返回 (33,1)。
  • 在熊猫 1.1.4 中:read_excel() got an unexpected keyword argument 'skip_blank_lines'
【解决方案2】:

skip_blank_lines 参数在较新的 pandas 版本中无效。使用代码,如获取 excel 中的精确 df。

df = pd.read_excel('book1.xlsx',dtype="str").fillna('')

【讨论】:

    猜你喜欢
    • 2017-01-13
    • 2018-05-21
    • 2017-06-10
    • 2018-05-31
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    相关资源
    最近更新 更多