NULL.Dude
NULL.Dude

Reputation: 219

Excel file to Python dictionary

I have an Excel table where Column A is a list of emails and column B is a customer ID.

I need to create a Python dictionary and have key=email address and value = customer ID.

Desired results:

dict = {[email protected] :'customer ID'}

My code is below:

import pandas as pd
excel = "excel_file.xlsx"
list_dict = pd.read_excel(excel, index_col=0).to_dict()
print list_dict

However the dictionary is printing like this:

{u'customer ID': {u'[email protected]': u'customer ID}}

What am I doing wrong here?

Upvotes: 0

Views: 84

Answers (1)

minecraftplayer1234
minecraftplayer1234

Reputation: 2227

If your excel file looks like this:

enter image description here

Then you could do:

import pandas as pd
excel = "excel_file.xlsx"
list_dict = pd.read_excel(excel, header=None).to_dict('list')
print {k: v for k, v in zip(*list_dict.values())}

(tested on Python3, im sure it works in Python2 as well -> be careful about Unicode)

Output:

C:\projects\python\test\so_pandas_dict>python main.py
{'[email protected]': 'customer ID', '[email protected]': 'customer ID1'}

Upvotes: 1

Related Questions