How to calculate mean values grouped on another column in Pandas

Rafa :

For the following dataframe:

StationID  HoursAhead    BiasTemp  
SS0279           0          10
SS0279           1          20
KEOPS            0          0
KEOPS            1          5
BB               0          5
BB               1          5

I'd like to get something like:

StationID  BiasTemp  
SS0279     15
KEOPS      2.5
BB         5

I know I can script something like this to get the desired result:

def transform_DF(old_df,col):
    list_stations = list(set(old_df['StationID'].values.tolist()))
    header = list(old_df.columns.values)
    header.remove(col)
    header_new = header
    new_df = pandas.DataFrame(columns = header_new)
    for i,station in enumerate(list_stations):
        general_results = old_df[(old_df['StationID'] == station)].describe()
        new_row = []
        for column in header_new:
            if column in ['StationID']: 
                new_row.append(station)
                continue
            new_row.append(general_results[column]['mean'])
        new_df.loc[i] = new_row
    return new_df

But I wonder if there is something more straightforward in pandas.

Zero :

You could groupby on StationID and then take mean() on BiasTemp. To output Dataframe, use as_index=False

In [4]: df.groupby('StationID', as_index=False)['BiasTemp'].mean()
Out[4]:
  StationID  BiasTemp
0        BB       5.0
1     KEOPS       2.5
2    SS0279      15.0

Without as_index=False, it returns a Series instead

In [5]: df.groupby('StationID')['BiasTemp'].mean()
Out[5]:
StationID
BB            5.0
KEOPS         2.5
SS0279       15.0
Name: BiasTemp, dtype: float64

Read more about groupby in this pydata tutorial.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas: compute the mean of a column grouped by another column

From Dev

How to add a column with given values to a Pandas dataframe, grouped by another column?

From Dev

How to get mean of rows selected with another column's values in pandas

From Dev

How to get mean of selected rows with another column's values in pandas

From Dev

how to calculate mean values of the past n years grouped by two variables

From Dev

Normalize column in pandas dataframe by sum of grouped values of another column

From Dev

Calculate mean from a column based on another column in pandas

From Dev

How to sum values grouped by a categorical column in pandas?

From Dev

How to calculate mean of grouped dataframe?

From Python

How to replace NaN values with another column's mean based on value in another column? Pandas

From Dev

How to select values grouped by column if another column has multiple values

From Dev

In Pandas, how to calculate the probability of a set of values in one column given a set of values of another column?

From Dev

How can I calculate a ratio that express the proportionality grouped by another column?

From Dev

Pandas Grouping - Values as Percent of Grouped Totals Based on Another Column

From Dev

Pandas - Calculate mean of a selection of cells from another column

From Dev

Pandas/Python groupby and then calculate mean for another column within each group

From Dev

Groupby multiple columns count size and calculate mean of another column in Pandas

From Dev

How can I calculate a grouped and weighted aggregate of a column in pandas?

From Dev

In pandas, how to create a new column with a rank according to the mean values of another column

From Dev

How to mean columns by values of another column?

From Dev

How to calculate the values of a pandas DataFrame column depending on the results of a rolling function from another column

From Dev

In Pandas, how to calculate the relative probabilities of values of a column given a value of another column?

From Dev

Calculate column values in pandas based on previous rows of data in another column

From Dev

Calculate average of specified range of values in pandas column and store as another column

From Dev

Calculate the sum of a pandas column depending on the change of values in another column

From Dev

How to plot a graph of the values in a column in Pandas DataFrame grouped by a different column

From Dev

How to calculate ratio of values in a pandas dataframe column?

From Dev

How to calculate a mean depending on another logical column using dplyr?

From Dev

How to return value_counts() when grouped by another column in pandas

Related Related

  1. 1

    Pandas: compute the mean of a column grouped by another column

  2. 2

    How to add a column with given values to a Pandas dataframe, grouped by another column?

  3. 3

    How to get mean of rows selected with another column's values in pandas

  4. 4

    How to get mean of selected rows with another column's values in pandas

  5. 5

    how to calculate mean values of the past n years grouped by two variables

  6. 6

    Normalize column in pandas dataframe by sum of grouped values of another column

  7. 7

    Calculate mean from a column based on another column in pandas

  8. 8

    How to sum values grouped by a categorical column in pandas?

  9. 9

    How to calculate mean of grouped dataframe?

  10. 10

    How to replace NaN values with another column's mean based on value in another column? Pandas

  11. 11

    How to select values grouped by column if another column has multiple values

  12. 12

    In Pandas, how to calculate the probability of a set of values in one column given a set of values of another column?

  13. 13

    How can I calculate a ratio that express the proportionality grouped by another column?

  14. 14

    Pandas Grouping - Values as Percent of Grouped Totals Based on Another Column

  15. 15

    Pandas - Calculate mean of a selection of cells from another column

  16. 16

    Pandas/Python groupby and then calculate mean for another column within each group

  17. 17

    Groupby multiple columns count size and calculate mean of another column in Pandas

  18. 18

    How can I calculate a grouped and weighted aggregate of a column in pandas?

  19. 19

    In pandas, how to create a new column with a rank according to the mean values of another column

  20. 20

    How to mean columns by values of another column?

  21. 21

    How to calculate the values of a pandas DataFrame column depending on the results of a rolling function from another column

  22. 22

    In Pandas, how to calculate the relative probabilities of values of a column given a value of another column?

  23. 23

    Calculate column values in pandas based on previous rows of data in another column

  24. 24

    Calculate average of specified range of values in pandas column and store as another column

  25. 25

    Calculate the sum of a pandas column depending on the change of values in another column

  26. 26

    How to plot a graph of the values in a column in Pandas DataFrame grouped by a different column

  27. 27

    How to calculate ratio of values in a pandas dataframe column?

  28. 28

    How to calculate a mean depending on another logical column using dplyr?

  29. 29

    How to return value_counts() when grouped by another column in pandas

HotTag

Archive