Pandas how to calculate sum for columns in list x and mean for columns in list y using aggregate

rams

I have a dataframe with 60+ columns. I want to calculate sum for the column names starting with 'FLAG_' and mean for the column names starting with 'RESPONSE_' . How to achieve this? I tried using agg() with no luck.

Note: I'm trying to achieve this as part of data pipeline

sum_cols = [col for col in df.columns if 'FLAG_' in col]
mean_cols = [col for col in df.columns if 'RESPONSE_' in col]
dict_ = {tuple(sum_cols): 'sum', tuple(mean_cols): 'mean'}
df = df.groupby('ID').agg(dict_)

but I'm getting error because while selecting the columns it should be a list rather tuple. since it's a dictionary I cannot pass list as key.

KeyError: "Column(s) [array([#<All the FLAG_ columns listed>], dtype=object)] do not exist"
rams
    sum_cols = [col for col in df.columns if 'FLAG_' in col]
    mean_cols = [col for col in df.columns if 'RESPONSE_' in col]
    dict_ = {col:'sum' for col in sum_cols}
    dict_.update({col:'mean' for col in mean_cols})
    df = df.groupby('ID').agg(dict_)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Python

pandas: how to aggregate two list columns when joining data frames

From Java

How to calculate the sum of the list using stream?

From Java

How to sum a column grouped by other columns in a list?

From Java

Can pandas groupby aggregate into a list, rather than sum, mean, etc?

From Dev

how to rename columns in pandas using a list

From Dev

How to get data from multiple columns and save it in a list using pandas?

From Dev

Python Pandas - Calculate mean using criteria from two columns

From Dev

Calculate the mean value using two columns in pandas

From Dev

pandas how to aggregate sum on a column depending on values in other columns

From Dev

How to order the columns in a Pandas Dataframe using a list of strings

From Dev

Pandas: aggregate sum showing list

From Dev

Calculate weighted sum using two columns in pandas dataframe

From Dev

How to calculate the mean of prefix rows and make it as the new columns in pandas?

From Dev

kdb - how to create sum a list of dynamic columns using functional select

From Dev

Calculate mean of multiple dataframes columns stored in List

From Dev

How to aggregate columns in pandas

From Dev

pandas: How to pivot multiple columns and calculate their sum?

From Dev

Using a list to specify pandas columns

From Dev

Sum the columns in a list of dataframes

From Dev

Sum of columns in list of objects

From Dev

pandas groupby aggregate for columns having lists of items returns string and not list

From Dev

Sum a list of Columns

From Dev

How to use list to calculate between columns

From Dev

Calculate mean of row using only certain columns in pandas

From Dev

pandas get the mean len of columns that is a list in groupby

From Dev

How to split data inside list into individual separate columns using pandas

From Dev

Pandas how to calculate mean for second level multi index columns

From Dev

How to calculate cumulative sum from different columns based on months using pandas dataframe?

From Dev

How to get a mean of a list of columns in a polars dataframe

Related Related

  1. 1

    pandas: how to aggregate two list columns when joining data frames

  2. 2

    How to calculate the sum of the list using stream?

  3. 3

    How to sum a column grouped by other columns in a list?

  4. 4

    Can pandas groupby aggregate into a list, rather than sum, mean, etc?

  5. 5

    how to rename columns in pandas using a list

  6. 6

    How to get data from multiple columns and save it in a list using pandas?

  7. 7

    Python Pandas - Calculate mean using criteria from two columns

  8. 8

    Calculate the mean value using two columns in pandas

  9. 9

    pandas how to aggregate sum on a column depending on values in other columns

  10. 10

    How to order the columns in a Pandas Dataframe using a list of strings

  11. 11

    Pandas: aggregate sum showing list

  12. 12

    Calculate weighted sum using two columns in pandas dataframe

  13. 13

    How to calculate the mean of prefix rows and make it as the new columns in pandas?

  14. 14

    kdb - how to create sum a list of dynamic columns using functional select

  15. 15

    Calculate mean of multiple dataframes columns stored in List

  16. 16

    How to aggregate columns in pandas

  17. 17

    pandas: How to pivot multiple columns and calculate their sum?

  18. 18

    Using a list to specify pandas columns

  19. 19

    Sum the columns in a list of dataframes

  20. 20

    Sum of columns in list of objects

  21. 21

    pandas groupby aggregate for columns having lists of items returns string and not list

  22. 22

    Sum a list of Columns

  23. 23

    How to use list to calculate between columns

  24. 24

    Calculate mean of row using only certain columns in pandas

  25. 25

    pandas get the mean len of columns that is a list in groupby

  26. 26

    How to split data inside list into individual separate columns using pandas

  27. 27

    Pandas how to calculate mean for second level multi index columns

  28. 28

    How to calculate cumulative sum from different columns based on months using pandas dataframe?

  29. 29

    How to get a mean of a list of columns in a polars dataframe

HotTag

Archive