在空闲时使用 Pandas

Using Pandas with Idle

我在 spyder 中编写的代码在 python (3.6) IDLE 中不起作用。

import pandas as pd

df = pd.read_excel('file.xlsx', usecols = ['A','B'])
print(df)

这应该打印 DF,但它只打印了一个空框。我已经安装了 Pandas 和 xlrd。如何在 IDLE 中完成这项工作?

编辑:

这是excel table.

Edit2:这是输出。

Empty DataFrame
Columns: []
Index: []

当我尝试编写带有 DF 错误的代码时。

代码示例:

keylist = []
list1, list2 = df['A'].tolist(), df['B'].tolist()

for i in zip(list1, list2):
    val = map(str, i)
    keylist.append('/'.join(val))

print(keylist)

KeyError: 'A'

我认为 python 试图用一个空的 DF 做一些事情。但是我怎样才能让它正确读取 excel 文件呢?

如果使用列名,我认为 usecols 需要是字符串:

df = pd.read_excel('file.xlsx', usecols = 'A,B')

这个我测试过,和上面说的一样。

来自文档:

usecols : int or list, default None

If None then parse all columns, If int then indicates last column to be parsed If list of ints then indicates list of column numbers to be parsed If string then indicates comma separated list of Excel column letters and column ranges (e.g. “A:E” or “A,C,E:F”). Ranges are inclusive of both sides.