Stefano Potter
Stefano Potter

Reputation: 3577

Subtracting after groupby

I have a dataframe like this:

   Allotment    Date        NDII_Mean
   Arnstson     19900619    0.073023
   A_Annex      19900619    0.131290
   Arnstson     19900620    0.045553
   A_Annex      19900620    0.688850

and I want to group by Allotment and then subtract the 19900620 dates by the 19900619 dates.

I want my output to look something like this:

Allotment      NDII_Mean
Arnstson       -0.02747
A_Annex         0.55756 

Upvotes: 0

Views: 1229

Answers (2)

chappers
chappers

Reputation: 2415

You can use reshape strategies (pivot) so that you can naturally subtract the result.

df = pd.DataFrame([['Arnstson' ,   19900619 ,  0.073023],
                   ['A_Annex'  ,   19900619 ,  0.131290],
                   ['Arnstson' ,   19900620 ,  0.045553],
                   ['A_Annex'  ,   19900620 ,  0.688850]],
                 columns=['Allotment', 'Date', 'NDII_Mean'])
dfreshape = df.pivot('Allotment', 'Date')    
#           NDII_Mean          
# Date       19900619  19900620
# Allotment                    
# A_Annex    0.131290  0.688850
# Arnstson   0.073023  0.045553    

You can then simply use your index/slicing to get the desired result:

dfreshape['NDII_Mean',19900620] - dfreshape['NDII_Mean',19900619]
# Allotment
# A_Annex     0.55756
# Arnstson   -0.02747
# dtype: float64

Full code:

dfreshape = df.pivot('Allotment', 'Date')   
dfreshape['NDII_Mean',19900620] - dfreshape['NDII_Mean',19900619]

Upvotes: 1

Boa
Boa

Reputation: 2677

difference = lambda x: ['x['Allotment'][0], x.ix[1]['NDII_Mean'] - x.ix[0]['NDII_Mean']]
df_diffs = DataFrame([difference(x[1].reset_index(drop = True)) for x in df.groupby(['Allotment'])])
df_diffs.columns = ['Allotment', 'NDII_Mean']
print df_diffs

  Allotment  NDII_Mean
0   A_Annex    0.55756
1  Arnstson   -0.02747

Upvotes: 1

Related Questions