Pandas Calculate Average Bias By Rows from 2 Columns

user2100039

I have a dataframe that looks like this below and I am trying to calculate a simple bias by comparing two columns of data - the column 'obsvals' and 'modelvals'. I need to subtract 'obsvals' from 'modelvals' at each month and sum those differences to compute the months 1 and 2 cumulative bias. I'm not sure how to do that in python. I'm guessing a combination of using groupby 'plant_name' and maybe a lambda function..?

Here is the dataframe:

     plant_name  year  month  obsvals  modelvals  Bias
0     ARIZONA I  2021      1     8.90       8.30  0.60
1     ARIZONA I  2021      2     7.98       7.41  0.57
3     CAETITE I  2021      1     9.10       7.78  1.32
4     CAETITE I  2021      2     6.05       6.02  0.03 

My final answer should look like:

     plant_name  year  Bias
0    ARIZONA I   2021   0.58
1    CAETITE I   2021   0.67

thank you for your time,

Nk03

IIUC, you need groupby:

df = df.groupby(['plant_name','year']).agg({'Bias': np.mean}).reset_index()

OUTPUT:

  plant_name  year   Bias
0   ARIZONAI  2021  0.585
1   CAETITEI  2021  0.675

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Calculate average of every n rows from a csv file

Python, Pandas: average every 2 rows together

pandas: Calculate the difference from a grouped average

Calculate average of columns and rows

calculate average value from pandas dataframe

Add new rows to calculate the sum and average from exiting pandas dataframe

Average a subset of rows across multiple pandas columns

Calculate the number of different columns between rows in Pandas

Calculate Average between columns by comparing two rows in SQL Server

Pandas: Calculate the average every 2 rows of a column and put it into the a new column

Calculate weighted average based on 2 columns using a pandas/dataframe

Extract rows and calculate average

Calculate average and standard deviation per 5 rows in a pandas dataframe

Calculate average of non numeric columns in pandas

Get average of columns in pandas based on particular rows

Calculate average of values between 2 columns sql

calculate average and maximum value for subset of rows in pandas dataframe

Calculate average from multiple rows

Subtracting average values from columns in pandas DataFrame

Calculate average time using 2 columns from separate MySQL Tables

Fetch values from the selected rows and calculate its average

pandas, How to add rows with average grouped columns

Pandas calculate a new column from multiple other columns and subset of rows

How to calculate moving average for each subsets of rows in pandas dataframe?

Calculate the average consumption from data in Pandas Dataframe

Calculate weighted average results for multiple columns based on another dataframe in Pandas

calculate average if all the columns have value in Pandas

Calculate average of every n rows in pandas and assign new labels to rows

Create Rows+Columns from 2 lists in pandas DataFrame