Currently I have table in this format :
Geo-id Name stat Year index 111500 Anniston-Oxford-Jacksonville AL 1991 0 111500 Anniston-Oxford-Jacksonville AL 1992 91.675 111500 Anniston-Oxford-Jacksonville AL 1993 93.8025 111500 Anniston-Oxford-Jacksonville AL 1994 96.63 111500 Anniston-Oxford-Jacksonville AL 1995 99.455 111500 Anniston-Oxford-Jacksonville AL 1996 102.4875 111500 Anniston-Oxford-Jacksonville AL 1997 109.0225 111500 Anniston-Oxford-Jacksonville AL 1998 114.7075 111500 Anniston-Oxford-Jacksonville AL 1999 116.005 112220 Auburn-Opelika AL 1992 90.695 112220 Auburn-Opelika AL 1993 94.2075 112220 Auburn-Opelika AL 1994 98.6825 112220 Auburn-Opelika AL 1995 103.3675 112220 Auburn-Opelika AL 1996 107.2725 112220 Auburn-Opelika AL 1997 111.7125
This should be converted to :
Geo-id Name 1991 1992 1993 1994 ........... 2017 111500 Anniston-Oxford-Jacksonville 0 91.675 93.8025 96.63 112220 Auburn-Opelika 0 90.695 94.2075 98.6825 and so on .....
Keeping geoid and name vertical ( but repeating only once since it is pivoted horizontally).
Code I have so far :
pre_horizontal_df = pd.read_csv('database_raw.csv')
pre_horizontal_df['period'] = pre_horizontal_df.year.astype(str)
df1 = pre_horizontal_df.groupby(['geoid', 'name'])['hpi'].mean().unstack()
print (df1)
But this is not working. Can this horizontal pivot be done in python df / pandas?