How to group specific items in a column and calculate the mean

HoneyWeGOTissues

I am trying to figure out how to group specific tag names in a column and then to calculate the mean of the raw data that have the same time. My dataframe looks something like this except with 10000+ rows:

tag_name time raw_data
happy     5      300
          8      340
angry     5      315
          8      349
sad       5      400
          8      480
etc.
.
.

I wish to keep the mean in the dataframe table, but I can't figure out how. I found that I can create a pivot table to calculate the mean, but I can't figure out how to single out the specific tag names I want. Below is what I have so far:

output = pd.pivot_table(data=dataset,index=['Timestamp'],columns=['Tag_Name'],values='Raw_Data',aggfunc='mean')

I am trying to get one of these outputs when I calculate the average of sad and happy:

1. optimal output:
tag_name    time raw_data  sad_happy_avg
happy        5      300     350
             8      340     410
sad          5      400
             8      480
angry        5      315
             8      349

2. alright output:
tag_name   happy   sad  avg
time
5           300    400  350
8           340    480  410
ouroboros1

Try as follows:

  • Use Series.isin to keep only "happy" and "sad", and apply df.pivot to get the data in the correct shape.
  • Next, add a column for the mean on axis=1:
res = df[df.tag_name.isin(['happy','sad'])].pivot(index='time', columns='tag_name', 
                                       values='raw_data')
res['avg'] = res.mean(axis=1)

print(res)

tag_name  happy  sad    avg
time                       
5           300  400  350.0
8           340  480  410.0

Your "optimal" output doesn't seem a very logical way to present/store this data, but you can achieve it as follows:

# assuming you have a "standard" index starting `0,1,2` etc. 
df['sad_happy_avg'] = df[df.tag_name.isin(['happy','sad'])]\
    .groupby('time')['raw_data'].mean().reset_index(drop=True)

print(df)

  tag_name  time  raw_data  sad_happy_avg
0    happy     5       300          350.0
1    happy     8       340          410.0
2    angry     5       315            NaN
3    angry     8       349            NaN
4      sad     5       400            NaN
5      sad     8       480            NaN

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

in R, how to calculate mean of all column, by group?

From Dev

How to calculate mean of years of a specific column?

From Dev

How to group, sum up and calculate a mean of every other element in a column?

From Dev

How to calculate the mean of a new group?

From Dev

Pandas: calculate mean by specific value in column with a for loop

From Dev

How to calculate mean in group by another group?

From Dev

How to calculate a mean by group in a JS Array?

From Dev

How to calculate mean spatial location by group

From Dev

How can I calculate the mean over a series of specific, constant width column ranges?

From Dev

How to calculate the mean of a specific value in columns in Python?

From Dev

How could I calculate the variance with specific mean

From Dev

How to calculate mean of specific rows in python dataframe?

From Dev

How to calculate rolling mean for each column

From Dev

How to calculate the mean for a column subsetted from the data

From Dev

How to calculate the mean value of the column inside the function

From Java

How do I calculate the mean of a column

From Dev

How to calculate mean of matrix based on column value

From Dev

How to Further Group Items in a Column After Group By

From Dev

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

From Dev

Calculate Group Mean and Overall Mean

From Dev

Calculate mean by group with dplyr

From Dev

I want to calculate the mean of a pandas column for specific months

From Dev

Calculate the mean of specific rows

From Dev

How to Group by the mean of specific columns in Python

From Dev

How to group list items based on a specific condition?

From Dev

Create mean column for specific columns depending on group in R

From Dev

how to calculate number of days in a specific column in pandas

From Python

How to group by two columns, calculate weighted mean, return DataFrame, in Python

From Dev

How to calculate mean value per group in Teradata SQL?

Related Related

  1. 1

    in R, how to calculate mean of all column, by group?

  2. 2

    How to calculate mean of years of a specific column?

  3. 3

    How to group, sum up and calculate a mean of every other element in a column?

  4. 4

    How to calculate the mean of a new group?

  5. 5

    Pandas: calculate mean by specific value in column with a for loop

  6. 6

    How to calculate mean in group by another group?

  7. 7

    How to calculate a mean by group in a JS Array?

  8. 8

    How to calculate mean spatial location by group

  9. 9

    How can I calculate the mean over a series of specific, constant width column ranges?

  10. 10

    How to calculate the mean of a specific value in columns in Python?

  11. 11

    How could I calculate the variance with specific mean

  12. 12

    How to calculate mean of specific rows in python dataframe?

  13. 13

    How to calculate rolling mean for each column

  14. 14

    How to calculate the mean for a column subsetted from the data

  15. 15

    How to calculate the mean value of the column inside the function

  16. 16

    How do I calculate the mean of a column

  17. 17

    How to calculate mean of matrix based on column value

  18. 18

    How to Further Group Items in a Column After Group By

  19. 19

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

  20. 20

    Calculate Group Mean and Overall Mean

  21. 21

    Calculate mean by group with dplyr

  22. 22

    I want to calculate the mean of a pandas column for specific months

  23. 23

    Calculate the mean of specific rows

  24. 24

    How to Group by the mean of specific columns in Python

  25. 25

    How to group list items based on a specific condition?

  26. 26

    Create mean column for specific columns depending on group in R

  27. 27

    how to calculate number of days in a specific column in pandas

  28. 28

    How to group by two columns, calculate weighted mean, return DataFrame, in Python

  29. 29

    How to calculate mean value per group in Teradata SQL?

HotTag

Archive