0

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@domain.com :'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@domain.com': u'customer ID}}

What am I doing wrong here?

jpp
  • 159,742
  • 34
  • 281
  • 339
NULL.Dude
  • 219
  • 1
  • 13
  • Read https://stackoverflow.com/questions/2464959/whats-the-u-prefix-in-a-python-string – Kamal Nayan Jul 04 '18 at 13:45
  • Possible duplicate of [What's the u prefix in a Python string?](https://stackoverflow.com/questions/2464959/whats-the-u-prefix-in-a-python-string) – jpp Jul 04 '18 at 14:04
  • For making this question self-contained, please post a short snippet that constructs the DataFrame with 2-3 lines in-place, without having to refer to a unavailable Excel file. Your question seems to be mostly about the to-dict conversion, not the input format. (Hint: simply reproducing the output of `df.head()` returned by `pd.read_excel(...)` should be enough.) – ojdo Jul 04 '18 at 14:09

1 Answers1

1

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
{'example@gmail.com': 'customer ID', 'example1@gmail.com': 'customer ID1'}
minecraftplayer1234
  • 2,127
  • 4
  • 27
  • 57