3

I have a very specific Excel sheet that looks like this:

Excel Sheet Picture

I am trying to convert it to a Python dictionary like this:

item_map = {
    "1609755": {
        "name": "test 1",
        "price": 125,
        "check_type": "check 1"
    },
    "1609756": {
        "name": "test 2",
        "price": 2500,
        "check_type": "check 2"
    },
    "1609758": {
        "name": "test 3",
        "price": 2400,
        "check_type": "check 3"
    }
}

My question is: How do I convert a excel sheet to a Python dictionary?


So far I tried using the library sheet2dict and also pandas, however in the excel sheet, the id is a key in the dictionary. As a result, I am quite stumped on how to ensure that the first column is ALWAYS set as a key.

from sheet2dict import Worksheet
ws = Worksheet()
file_path = 'test.xlsx'

x = ws.xlsx_to_dict(path = file_path)

print(x)

1 Answer 1

1

Since I do not have the Excel file, I created the DataFrame using your dictionary mentioned in the question i.e, item_map likewise:

df = pd.DataFrame(item_map).T    #Had to take a Transpose
df.index.name = 'id'

You can import your table as a DataFrame using pandas.read_excel. Remember to set the index as 'id'.

df = pd.read_excel('file_path')
df.index.name = 'id'

Then, use this code:

item_map = {}

for index, row in list(df.iterrows()):
    item_map[index] = dict(row)

print(item_map)

Output:

{'1609755': {'name': 'test 1', 'price': 125, 'check_type': 'check 1'},
'1609756': {'name': 'test 2', 'price': 2500, 'check_type': 'check 2'},
'1609758': {'name': 'test 3', 'price': 2400, 'check_type': 'check 3'}}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, however it seems after importing my table as a dataframe, it automatically specifies the key as 0, 1, 2. How do I specify it as the first column in my Excel file?
Actually I figured it out: df.set_index('id', inplace=True) will set it so the id is the key.
Yes, did it work ? I tested my code by recreating your DataFrame.I used the dictionary you have mentioned in your question likewise: df = pd.DataFrame(item_map) .T and then set the index as: df.index.name = 'id'
@ShivamRoy: The initial example only give a flat structure, ie {0: {'Id': 751, 'Name': 'Test 1', 'Price': 125, 'check_type': 'Check 1'}, Please update the answer with a working solution with nested dicts as OP asked for.
@IODEV The output would look flat actually, but it is in exactly the same format OP asked for. I tried the code before posting it and it did return me the nested dictionary. I'm sorry if I didn't understand your issue, is it regarding the alignment of the lines ? If you run the code you'll find that the output is in the same nested format as OP asked for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.