I am trying to convert the following o/p of my SQL query (as a dataframe) to a dictionary of list:
Column A Column B
M N
M O
I want the result in following format:
{M:[N,O]}
Can someone help me in this?
I am trying to convert the following o/p of my SQL query (as a dataframe) to a dictionary of list:
Column A Column B
M N
M O
I want the result in following format:
{M:[N,O]}
Can someone help me in this?
You could try like this:
a_dict = df.groupby('Column A', as_index=True)['Column B'].apply(list).to_dict()
print(a_dict)
{'M': ['N', 'O']}
This worked for me:
tst = data.groupby("a", as_index =False).agg("|".join)
tst['b'] = tst['b'].apply(lambda x: x.split("|"))
a, b = tst['a'].tolist(), tst['b'].tolist()
your_dict = dict(zip(a,b))
and vola
Please note that this is a round about way, and there is probably a better solution.