Python pandas dataframe for dictionary

I have a two-frame dataframe and I intend to convert it to a python dictionary - the first column will be the key, and the second will be the value. Thank you in advance.

Dataframe:

id value 0 0 10.2 1 1 5.7 2 2 7.4 
+47
python dictionary pandas
Sep 09 '13 at 9:49 on
source share
5 answers

See docs for to_dict . You can use it as follows:

 df.set_index('id').to_dict() 

And if you have only one column to avoid the column name, this is also the level in the dict (in fact, in this case you are using Series.to_dict() ):

 df.set_index('id')['value'].to_dict() 
+82
Sep 09 '13 at 9:55 on
source share
β€” -

If you want an easy way to keep duplicates, you can use groupby :

 >>> ptest = pd.DataFrame([['a',1],['a',2],['b',3]], columns=['id', 'value']) >>> ptest id value 0 a 1 1 a 2 2 b 3 >>> {k: g["value"].tolist() for k,g in ptest.groupby("id")} {'a': [1, 2], 'b': [3]} 
+31
Jun 23 '14 at 16:08
source share
 mydict = dict(zip(df.id, df.value)) 
+29
03 Oct '16 at 17:41
source share

The joris answers in this thread and punchagan in the duplicated stream are very elegant, however they will not give the correct results if the column used for keys contains any duplicate value.

For example:

 >>> ptest = p.DataFrame([['a',1],['a',2],['b',3]], columns=['id', 'value']) >>> ptest id value 0 a 1 1 a 2 2 b 3 # note that in both cases the association a->1 is lost: >>> ptest.set_index('id')['value'].to_dict() {'a': 2, 'b': 3} >>> dict(zip(ptest.id, ptest.value)) {'a': 2, 'b': 3} 

If you have duplicate entries and don’t want to lose them, you can use this ugly but working code:

 >>> mydict = {} >>> for x in range(len(ptest)): ... currentid = ptest.iloc[x,0] ... currentvalue = ptest.iloc[x,1] ... mydict.setdefault(currentid, []) ... mydict[currentid].append(currentvalue) >>> mydict {'a': [1, 2], 'b': [3]} 
+16
Jun 23 '14 at 2:35
source share

Another (slightly shorter) solution for not playing duplicate records:

 >>> ptest = pd.DataFrame([['a',1],['a',2],['b',3]], columns=['id','value']) >>> ptest id value 0 a 1 1 a 2 2 b 3 >>> pdict = dict() >>> for i in ptest['id'].unique().tolist(): ... ptest_slice = ptest[ptest['id'] == i] ... pdict[i] = ptest_slice['value'].tolist() ... >>> pdict {'b': [3], 'a': [1, 2]} 
0
Oct 23 '17 at 16:29
source share



All Articles