kshnkvn
kshnkvn

Reputation: 956

How to read excel table with one column?

I have a table in Excel with one column that I want to read into the list: table

At first I tried it like this:

>>> df = pandas.read_excel('emails.xlsx', sheet_name=None)
>>> df
OrderedDict([('Sheet1',                [email protected]
0                [email protected]
1                  [email protected]
2    [email protected]
>>> for k, v in df.items():
...     print(type(v), v)
...
<class 'pandas.core.frame.DataFrame'>                [email protected]
0                [email protected]
1                  [email protected]
2    [email protected]
>>> df = df.items()[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'odict_items' object is not subscriptable

I tried it differently:

>>> df = pandas.read_excel('emails.xlsx', index_col=0)
>>> df
Empty DataFrame
Columns: []
Index: [[email protected], [email protected], [email protected]]

[419 rows x 0 columns]
>>> foo = []
>>> for i in df.index:
...     foo.append(i)
...
>>> foo
['[email protected]', '[email protected]', '[email protected]']

It almost worked, but the first element is missing. What else can I do? Is there really no way to read the Excel file simply line by line?

Upvotes: 0

Views: 90

Answers (2)

Parth
Parth

Reputation: 644

Try this:

df=pd.read_excel('temp.xlsx', header=None)
target_list=list(df[0].values)

Upvotes: 3

jezrael
jezrael

Reputation: 862611

Use:

target_list = pandas.read_excel('emails.xlsx', index_col=None, names=['A'])['A'].tolist()

Upvotes: 1

Related Questions