0

I am trying to create a plotly interactive map but am struggling to get the DataFrame into the correct format.

My df looks as such:

enter image description here

However the problem I am having is that most choropleth maps require the year to be in a single column with the value in another column. Is there a way to merge the years into a single solumn and have each value for the particular region in a column next to it?

I cannot simple transpose as this would then turn the Region name into a row which is not desired.

Any help would be appreciated as I cannot get my head around this.

Thanks.

geds133
  • 1,503
  • 5
  • 20
  • 52
  • 1
    Images aren't helpful because they take far too much time to time for us to recreate the problem. See [how to make a good pandas example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). On the other hand you might be looking for `df.set_index(['NUTS level', 'NUTS code', 'Region name']).stack()` – ALollz May 04 '20 at 14:55
  • Hi, I understand this, however I cannot simply copy the `df` as the view obscures most of the columns and therefore you cannot see how it looks. – geds133 May 04 '20 at 14:57
  • @ALollz, This is extremly close to what I am looking for, however the output is a Series and therefore if I was trying to capture a particular year by df[df['year'] == '2017'], I don't think this would work? – geds133 May 04 '20 at 14:59
  • 1
    Yes, you'll get a MultiIndexed Series. You can tack on a `.reset_index()` and then you'll be back to your DataFrame though you might get some weird columns names (`'level_0'`) that you can rename. Otherwise you can try [`melt`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.melt.html), which makes the renaming a bit easier. – ALollz May 04 '20 at 15:05

0 Answers0