r/pythonhelp • • Dec 13 '20

Converting Dictionary into hashable string

Hello, in trying to groupby a dataframe of New York City data by Borough, I am hit with the TypeError: unhashable type: 'dict'.

For background, I read a NYC Parks Department JSON file into Pandas that contains data on all the Parks Department big capital projects. Even though I'm sure this will be abundantly clear to you, I am very much a beginner, taking a 1.5 credit class on Python coding where my final project is using some open data source to answer a question--mine is trying to uncover any trends in completion rates and project size by NYC borough.

Below are the two different ways I tried it before realizing I needed to change the 'Boroughs' column to...not a dictionary

by_borough = df.groupby('Boroughs').size().reset_index(name='Projects')
by_borough

borough_phases = df.groupby(['CurrentPhase','Boroughs']).size().reset_index(name='Projects')
borough_phases

Key-value pairing below...

I've been researching this for over two hours and I know that I must be missing some key point of what makes a dictionary a dictionary. I just want to be able to make it mutable/hashable so that I make a df and table that looks something like this:

phases = capital_projects.groupby('CurrentPhase').size().to_frame('Projects')
phases

I don't even know how to ask this question correctly in Google, so everything I've tried to find and tried just gives me more errors. Any and all help would be so appreciated.

Thanks.

5 Upvotes

1 comment sorted by

View all comments

1

u/MT1961 Dec 14 '20

From the df and other code, I'm guessing your are using Pandas (i.e. dataframes and the like) but that has little to do with your problem. Dictionary keys must be immutable, which I think you figured out. The usual way around this is to convert them to tuples.

if you need more help, I'd need to see a bit more code.