Reputation: 219
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
Reputation: 2227
If your excel file looks like this:
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