I'd like to use to some 'unpivot' method in python to reshape my pandas data frame. However, one of the columns that I want to keep is a 'geometry' column of geopandas after reshaping it. My dataset has 5 columns and there's no way to put this 'geometry' column as index and then resetting the index just as I tried below:
us_tidy[['Name','STATE_ABBR','geometry','Year','Income']].pivot_table(index = ['Name','STATE_ABBR', 'geometry'], columns = 'Year', values = 'Income').reset_index()
Is there a way to reshape the dataset in this wide way and keeping some columns but AVOIDING to have this index part (at least for this specific column)???
Here's some lines of the geopandas dataset:
In addition, here's some reproducible example:
import pandas as pd
import pysal as ps
import geopandas as gpd
csv_path = ps.examples.get_path('usjoin.csv')
usjoin = pd.read_csv(csv_path)
years = list(range(1929, 2010))
cols_to_calculate = list(map(str, years))
shp_path = ps.examples.get_path('us48.shp')
us48_map = gpd.read_file(shp_path)
us48_map = us48_map[['STATE_FIPS','STATE_ABBR','geometry']]
us48_map.STATE_FIPS = us48_map.STATE_FIPS.astype(int)
df_map = us48_map.merge(usjoin, on='STATE_FIPS')
# Making the dataset tidy
us_tidy = pd.melt(df_map,
id_vars=['Name', 'STATE_FIPS', 'STATE_ABBR', 'geometry'],
value_vars=cols_to_calculate,
var_name='Year',
value_name='Income').\
sort_values('Name')
us_tidy[['Name','STATE_ABBR', 'geometry', 'Year','Income']].\
pivot_table(index = ['Name','STATE_ABBR', 'geometry'],
columns = 'Year',
values = 'Income')
The error message is: TypeError: unhashable type: 'Polygon'
Best regards, Renan