0

I have a dataframe of a collection of data and i have it grouped by 2 different groups not sure how to go about doing calculations or even pull the specific cell I'm looking at

ZipCode(group1) Year(group2)    Valuex         x2        x3

10001 2012                       1              7        12
      2013                       5              1        13
      2014                       6              8        15

10002 2012                       x              x         x
      2013                       x              x         x
      2014                       x              x         x

So I am still learning about DataFrames; I really don't know where to go from here, I can do .sum() or .mean() but it's not what I'm looking to do.

I'm looking to grab the first and last year of each zip and get the change in percent.

So for zipcode = 10001 I would want to pull 2014x -2012x/2012x

But for each zipcode, and if left ungrouped the zips repeat in the 100 thousands

this is my actual dataframe:

DF

Example would be zcta5 00601 Median Home Value change over time would be its 2017 value - its 2012 value divided by 2012 value

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    please provide your input dataframe & the expected dataframe, that is much easier to understand – moys Sep 04 '19 at 15:53
  • i was told the code wasnt minimal but this is what the data frame looks like, https://gyazo.com/77ff9a29aafe1cce09e1d7e38fedb917 i am trying to get the change in value for each zip from the first year of collected data to last year of collected data for each row – Michael Padilla Sep 04 '19 at 15:59
  • Possible duplicate of [How to calculate percent change compared to the beginning value using pandas?](https://stackoverflow.com/questions/35090498/how-to-calculate-percent-change-compared-to-the-beginning-value-using-pandas) – G. Anderson Sep 04 '19 at 16:09

1 Answers1

0

It's probably easier to solve this problem while groupby than after groupby. However, you can also try:

df = df.swaplevel(0,1) 
df.loc[2017].sub(df.loc[2012]).div(df.loc[2012])
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • yeah cant use swaplevel on groupby frame, but doing it prior to the group by looks something like this (before any calculation) so i didnt know where i would begin in actually getting the first and last year https://gyazo.com/65eea6c1c606de35aab56bd5b3ddcd98 – Michael Padilla Sep 04 '19 at 16:32
  • I tested on my system and I could do `swaplevel` just fine. – Quang Hoang Sep 04 '19 at 17:14
  • weird i kept getting use .apply() instead error. BUT i took your advice and what i did was i took each year using .query and put it into a df of its own, than i merged and made a seperate df of all the changes – Michael Padilla Sep 04 '19 at 18:12