python - Pandas calculate year over year (or any other index) change in rows -
say have dataframe such:
               b  c  d            ---  -- -- --  2012-01-01 aaa  11 22 33 2013-01-01 aaa  11 23 53 2014-01-01 aaa  11 78 96   2012-01-01 bbb  12 42 24 2013-01-01 bbb  13 97 91 2014-01-01 bbb  14 25 12    (index datetime)
i want figure out change in column c between first appearance of aaa , last (in case 78-22 = 56). doing pivoting table years columns across top , adding difference column. there better way without modifying table?
if has many groups, may consider using groupby. assume df dataframe work with.
a = df.groupby("a")["c"] newt = a.last() - a.first()  print(newt)   the result:
a aaa    56 bbb   -17 name: c, dtype: int64      
Comments
Post a Comment