Pandas: Group by, filter rows, get the mean

Makoto Miyazaki

In python I have a pandas data frame df like this:

 ID      Geo    Speed
123    False       40
123     True       90
123     True       80
123    False       50
123     True       10
456    False       10
456     True       90
456    False       40
456     True       80

I want to group df by ID, and filter out rows where Geo == False, and get the mean of Speed in the group. So the result should look like this.

 ID     Mean 
123       60  
456       85  

My attempt:

df.groupby('ID')["Geo" == False].Speed.mean()
df.groupby('ID').filter(lambda g: g.Geo == False)
df[df.Geo.groupby(df.ID) == False]

Neither of them worked. Any solutions? Thank you!

jezrael

Use ~ for inverting Falses to Trues for filtering by Falses by boolean indexing:

print (df[~df["Geo"]])
    ID    Geo  Speed
0  123  False     40
3  123  False     50
5  456  False     10
7  456  False     40

df = df[~df["Geo"]].groupby('ID', as_index=False).Speed.mean()
print (df)
    ID  Speed
0  123     45
1  456     25

And for filtering by Trues:

print (df[df["Geo"]])
    ID   Geo  Speed
1  123  True     90
2  123  True     80
4  123  True     10
6  456  True     90
8  456  True     80

df = df[df["Geo"]].groupby('ID', as_index=False).Speed.mean()
print (df)
    ID  Speed
0  123     60
1  456     85

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Group by column and get mean of the the group pandas

From Dev

How to filter a pandas dataframe to just the rows that show change within a group?

From Dev

How to filter rows by group

From Dev

Pandas: Get mean of different rows when columns are equal

From Dev

Plot with pandas: group and mean

From Java

Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

From Dev

Pandas: Filter to get rows which contain a character not on a specified list

From Dev

Pandas Dataframe: get average of first rows of each subgroup within a group

From Dev

Filter in group by in pandas

From Dev

Get rows with Group BY in MySQL

From Dev

Get rows with Group BY in MySQL

From Dev

Fill in rows to get the same amount in each group after group by. Pandas

From Dev

pandas DataFrame filter by rows and columns

From Dev

With pandas filter rows on sum of column

From Dev

How to get the mean for each group in pandas.dataframe like seaborn.factorplot

From Dev

pandas group by and concatenate n rows

From Dev

Pandas dataframe get all rows between zero(0) of mask column and get first and last row of each group

From Dev

Pandas get consecutive rows

From Dev

Find mean of the grouped rows of pandas dataframe

From Dev

AngularJS - Angular Filter Group By sum rows

From Dev

AngularJS - Angular Filter Group By sum rows

From Dev

PostgreSQL - Group by filter out specific rows

From Java

pandas get column average/mean

From Dev

Faster way to transform group with mean value in Pandas

From Dev

Faster way to transform group with mean value in Pandas

From Dev

Python pandas dataframe group mean filtered by condition

From Dev

Get Rows By Group MySQL Java

From Dev

Pandas: Can I filter a dataframe to get only rows with a 50% difference between each other?

From Dev

Get correct values of mean of matrix rows

Related Related

  1. 1

    Group by column and get mean of the the group pandas

  2. 2

    How to filter a pandas dataframe to just the rows that show change within a group?

  3. 3

    How to filter rows by group

  4. 4

    Pandas: Get mean of different rows when columns are equal

  5. 5

    Plot with pandas: group and mean

  6. 6

    Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

  7. 7

    Pandas: Filter to get rows which contain a character not on a specified list

  8. 8

    Pandas Dataframe: get average of first rows of each subgroup within a group

  9. 9

    Filter in group by in pandas

  10. 10

    Get rows with Group BY in MySQL

  11. 11

    Get rows with Group BY in MySQL

  12. 12

    Fill in rows to get the same amount in each group after group by. Pandas

  13. 13

    pandas DataFrame filter by rows and columns

  14. 14

    With pandas filter rows on sum of column

  15. 15

    How to get the mean for each group in pandas.dataframe like seaborn.factorplot

  16. 16

    pandas group by and concatenate n rows

  17. 17

    Pandas dataframe get all rows between zero(0) of mask column and get first and last row of each group

  18. 18

    Pandas get consecutive rows

  19. 19

    Find mean of the grouped rows of pandas dataframe

  20. 20

    AngularJS - Angular Filter Group By sum rows

  21. 21

    AngularJS - Angular Filter Group By sum rows

  22. 22

    PostgreSQL - Group by filter out specific rows

  23. 23

    pandas get column average/mean

  24. 24

    Faster way to transform group with mean value in Pandas

  25. 25

    Faster way to transform group with mean value in Pandas

  26. 26

    Python pandas dataframe group mean filtered by condition

  27. 27

    Get Rows By Group MySQL Java

  28. 28

    Pandas: Can I filter a dataframe to get only rows with a 50% difference between each other?

  29. 29

    Get correct values of mean of matrix rows

HotTag

Archive