Python Forum
Pandas dataframe: calculate metrics by year
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas dataframe: calculate metrics by year
#1
Hi

I have some dataframes with a very diferent number of years , similar to the following dataframe:

Date	obs	sim
6/12/2000	22.32	14.6
8/11/2000	19.82	13.4
10/10/2000	16.63	16.7
2/14/2001	11.92	14.8
10/1/2001	19.15	13.4
10/23/2001	14.42	16.3
11/9/2001	9.97	19.9
11/27/2001	10.75	12.4
12/18/2001	8.22	10.6
1/16/2002	7.72	11.2
2/20/2002	7.92	11
3/21/2003	15.43	15.8
4/18/2003	12.69	14.6
5/20/2003	16.46	17
I need to calculate the average mean error (AME) and other metrics by year, between the obs and sim columns. How can I solve this problem. Using groupy? Splitting the dataframe? Do you have some example?

Thank you
Reply
#2
This will work!

import numpy as np
import pandas as pd
from sklearn.metrics import r2_score, mean_squared_error

def r2_rmse( g ):
    r2 = r2_score( g['Actual'], g['Predicted'] )
    rmse = np.sqrt( mean_squared_error( g['Actual'], g['Predicted'] ) )
    return pd.Series( dict(  r2 = r2, rmse = rmse ) )

your_df.groupby( 'Type' ).apply( r2_rmse ).reset_index()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Add NER output to pandas dataframe dg3000 0 130 Apr-22-2024, 08:14 PM
Last Post: dg3000
  HTML Decoder pandas dataframe column mbrown009 3 1,061 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,131 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,449 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe comparing anto5 0 1,273 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,810 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,560 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,327 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 2,461 Jun-22-2021, 07:57 PM
Last Post: snippsat
  iretate over columns in df and calculate euclidean distance with one column in pandas Pit292 0 3,313 May-09-2021, 06:46 PM
Last Post: Pit292

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020