1

My pandas dataframe (df):

Pattern      Support

Bread         4
Milk          4
Bread,Milk    4

My code:

x = df.set_index('Pattern').to_dict()
print(x)

Output of my code:

{'Support':{'MILK': 4,'BREAD':4, 'BREAD,MILK':4}}

Expected Output:

{'MILK': 4,'BREAD':4, 'BREAD,MILK':4}

I do not want the name of the second column 'Support' to be the main key, just want to get the elements and the count as in the expected output. Is there any pythonic (python 3.5) way?

1 Answer 1

5

Packed too much in one line, but it will give you what you want.

list(df.set_index('Pattern').to_dict().values()).pop()
Sign up to request clarification or add additional context in comments.

1 Comment

It worked!! can you explain what does pop() function do here? @Hun

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.