DataFrame: Group by one column and average other columns

DeepNet

Say I have the following DataFrame:

data = pd.DataFrame({'id' : ['1','2','3','4','5'], 'group' : ['1','1','2','1','2'], 
      'state' : ['True','False','False','True','True'], 'value' : [11,12,5,8,3]})

I would like to create a new DataFrame, keeping 3 columns: groups ('1' or '2'), and averaging over the columns 'state' and 'value', hence the DataFrame would be:

grouped_averaged = pd.DataFrame({'group' : ['1','2'], 'average_state' : [0.66,0.5], 'value' : [7,3]})
Quang Hoang

You just need groupby:

data['state'] = data['state'].eq('True')
data.drop('id',axis=1).groupby('group', as_index=False).mean()

Output:

  group     state      value
0     1  0.666667  10.333333
1     2  0.500000   4.000000

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Group a dataframe on one column and take max from one column and its corresponding value from the other col

分類Dev

How to replace string values in one column with actual column values from other columns in the same dataframe?

分類Dev

Create a Column in a Dataframe Conditional on Other Columns

分類Dev

How to select multiple columns and group by one column

分類Dev

MYSQL condense group by two columns into group by one column with breakdown

分類Dev

Create a new dataframe column by comparing two other columns in different dataframes

分類Dev

dataframe columns as key and column data as value group by id in spark scala

分類Dev

How to add the number in one column based on the numbers in other columns

分類Dev

calculate correlation between one column and a selection of other columns without loop

分類Dev

How to get rows with min values in one column, grouped by other column, while keeping other columns?

分類Dev

Grouping DataFrame, filtering by group size and by value of column in one line?

分類Dev

how to split a list of values in one column of a dataframe into various columns equally

分類Dev

How to get all columns in one column with panda dataframe?

分類Dev

Appending columns of DataFrame to other DataFrame

分類Dev

Check if a value in one column in one dataframe is within the range between values in two columns in another dataframe

分類Dev

SQL - Group By 3 columns in same table with average

分類Dev

Groupby on a column and apply function on another column but keep first element of all other columns of dataframe

分類Dev

Subtracting average values from columns in pandas DataFrame

分類Dev

Calculate average of month and replace values of other column

分類Dev

SQL average of previous range of columns into current column

分類Dev

Create new dataframe with tuple of cartesian product of few columns as one column and keep remaining columns from the same row

分類Dev

Interlacing two columns but with other columns in the dataframe

分類Dev

Merging two pandas dataframes with common values that are presented in one dataframe as columns and on the other are in rows

分類Dev

import text file into one column and assign default value to other columns of mysql table

分類Dev

I have two dataset and need to comapre string from one data set columns with other dataset column in R

分類Dev

Pandas Dataframe group by, column with a list

分類Dev

How update one dataframe's column by matching columns in two different dataframes in Pandas

分類Dev

Calculating Average in Google doc based on values in other columns

分類Dev

Mysql get average and sum of columns and group by year & month

Related 関連記事

  1. 1

    Group a dataframe on one column and take max from one column and its corresponding value from the other col

  2. 2

    How to replace string values in one column with actual column values from other columns in the same dataframe?

  3. 3

    Create a Column in a Dataframe Conditional on Other Columns

  4. 4

    How to select multiple columns and group by one column

  5. 5

    MYSQL condense group by two columns into group by one column with breakdown

  6. 6

    Create a new dataframe column by comparing two other columns in different dataframes

  7. 7

    dataframe columns as key and column data as value group by id in spark scala

  8. 8

    How to add the number in one column based on the numbers in other columns

  9. 9

    calculate correlation between one column and a selection of other columns without loop

  10. 10

    How to get rows with min values in one column, grouped by other column, while keeping other columns?

  11. 11

    Grouping DataFrame, filtering by group size and by value of column in one line?

  12. 12

    how to split a list of values in one column of a dataframe into various columns equally

  13. 13

    How to get all columns in one column with panda dataframe?

  14. 14

    Appending columns of DataFrame to other DataFrame

  15. 15

    Check if a value in one column in one dataframe is within the range between values in two columns in another dataframe

  16. 16

    SQL - Group By 3 columns in same table with average

  17. 17

    Groupby on a column and apply function on another column but keep first element of all other columns of dataframe

  18. 18

    Subtracting average values from columns in pandas DataFrame

  19. 19

    Calculate average of month and replace values of other column

  20. 20

    SQL average of previous range of columns into current column

  21. 21

    Create new dataframe with tuple of cartesian product of few columns as one column and keep remaining columns from the same row

  22. 22

    Interlacing two columns but with other columns in the dataframe

  23. 23

    Merging two pandas dataframes with common values that are presented in one dataframe as columns and on the other are in rows

  24. 24

    import text file into one column and assign default value to other columns of mysql table

  25. 25

    I have two dataset and need to comapre string from one data set columns with other dataset column in R

  26. 26

    Pandas Dataframe group by, column with a list

  27. 27

    How update one dataframe's column by matching columns in two different dataframes in Pandas

  28. 28

    Calculating Average in Google doc based on values in other columns

  29. 29

    Mysql get average and sum of columns and group by year & month

ホットタグ

アーカイブ