1

I need to create a dictionary where each element is a row. The key represents a specific column and the values will be a list of the remaining column entries. Simple example below:

example df:

Col_A     Col_B    Col_C    Col_D
Roger      5        blue    house
Kim        2        green   car
Mike       10       red     bike

desired dictionary = {'Roger':['5','blue','house'],'Kim':['2','green','car'],'Mike':['10','red','bike']}

Any suggestions are appreciated!

2 Answers 2

4

Set the index to COl_A then transpose the frame and convert to dictionary with orientation set to list

df.set_index('Col_A').T.to_dict('list')

{'Roger': [5, 'blue', 'house'],
 'Kim': [2, 'green', 'car'],
 'Mike': [10, 'red', 'bike']}
Sign up to request clarification or add additional context in comments.

Comments

2

Try:

print(df.set_index("Col_A").apply(list, 1).to_dict())

Prints:

{'Roger': [5, 'blue', 'house'], 'Kim': [2, 'green', 'car'], 'Mike': [10, 'red', 'bike']}

Comments

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.