-1

Say I have a dictionary that looks like:

{('a','1'):2,
 ('a','2'):1,
 ('b','1'):3,
 ('b','2'):2}

I want to get a dataframe result that takes the highest value item for the first key ('a','b' in this case) and create a dataframe from the tuple key.

key    value
'a'      '1'
'b'      '1'

How can I do this in python?

jpp
  • 159,742
  • 34
  • 281
  • 339
CWeeks
  • 407
  • 1
  • 5
  • 15
  • What do you mean by take the "highest frequency key pair"? Is the value in the dictionary the frequency? – enumaris May 03 '18 at 20:09
  • yes, highest frequency refers to the value in the dictionary. for key value pair starting with 'a', highest frequency is 2; for 'b', 3. Thanks. – CWeeks May 03 '18 at 20:26

1 Answers1

1

Try the following:

# let d is your dictionary
df = pd.DataFrame(d.keys(),columns=['key','value'])
df['freq'] = pd.DataFrame(d.values())
df['freq'] = pd.DataFrame(list(d.values()))
df = df.loc[df[['key','freq']].groupby('key').idxmax().freq]
       .drop('freq',axis=1)

Out[]:
    key value
2   a   1
0   b   1

Reset the index if you want:

df = df.reset_index(drop=True)

Out[]:
    key value
0   a   1
1   b   1
R.yan
  • 2,214
  • 1
  • 16
  • 33