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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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!

1
2
3
4
5
6
7
8
9
10
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
Question [Solved] Formatting cells of a pandas dataframe into an OpenDocument ods spreadsheet Calab 1 484 Mar-01-2025, 04:51 AM
Last Post: Calab
  Find duplicates in a pandas dataframe list column on other rows Calab 2 1,907 Sep-18-2024, 07:38 PM
Last Post: Calab
  Find strings by index from a list of indexes in a different Pandas dataframe column Calab 3 1,535 Aug-26-2024, 04:52 PM
Last Post: Calab
  Add NER output to pandas dataframe dg3000 0 1,111 Apr-22-2024, 08:14 PM
Last Post: dg3000
  HTML Decoder pandas dataframe column mbrown009 3 2,565 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,958 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 2,030 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe comparing anto5 0 1,873 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 2,574 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 2,040 Jan-10-2022, 04:42 PM
Last Post: moduki1

Forum Jump:

User Panel Messages

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