1

I havefound this answer Convert a Pandas DataFrame to a dictionary which almost works for the following scenario, but I will explain why it does not.

example

nestedList = [['A', 'B'], ['A', 'C']]
df = pd.DataFrame(nestedList,columns=["1","2"])

Returns the following dataframe

   1  2
0  A  B
1  A  C

Which I would like to then use .to_dict() to convert to a dictionary with keys column 1 and list column 2

sampledict = {"A":["B","C"]}

using the linked example I set the column I would like to use as the key to column 1 and orient to list

sampledict = df.set_index('1').T.to_dict('list')

which returns

{'A': ['C']}

Because Dataframe columns are not unique once you transpose which is pointed out with the userwarning:

UserWarning: DataFrame columns are not unique, some columns will be omitted.

Which is kind of confusing to me so I dug a bit deeper and found Export pandas to dictionary by combining multiple row values which I was able to modify to get the answer I wanted with the following code:

d = {}
for i in df["1"].unique():
    d[i] = [df["2"][j] for j in df[df["1"]==i].index]

Which returns

{'A': ['B', 'C']}

Which is great, however I still do not understand what it is that they did, as there was little explanation. Could a user please explain or write this in a more verbose manner? as well as potentially suggest a more simple solution?

Tyler Cowan
  • 820
  • 4
  • 13
  • 35

0 Answers0