Search code examples
pythonpandasspyderpython-idle

Using Pandas with Idle


I wrote code in spyder which does not work in python (3.6) IDLE.

import pandas as pd

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

This should print the DF but it just prints an empty frame. I have installed Pandas and xlrd. How do I make this work in IDLE?

Edit:

This is the excel table.

Excel Table

Edit2: This is the output.

Empty DataFrame
Columns: []
Index: []

When I try to write code with the DF errors occur.

Code Example:

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'

I think python tries to do something with an empty DF. But How can I make it read the excel file properly?


Solution

  • I think usecols need to be String if using column names:

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

    I have tested this and as mentioned above is the case.

    From Documentation :

    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.