How do I calculate mean on filtered rows of a pandas dataframe and append means to all columns of original dataframe?

Mike

How can I calculate all column's mean to ONLY rows that aren't equal to zero and append a new row at the bottom with the averages with only one line of code? It doesn't have to be one line, but I'm wondering why this doesn't work?

The code below ignores the (df.bar != 0) piece

df = df.append(df[(df.bar != 0)].mean(numeric_only=True), ignore_index=True)

Example df:

    foo     bar     total
0   foo1    bar1    293.09
1   foo2    0       0
2   foo3    bar3    342.3

Current Result:

0   foo     bar     total
1   foo1    bar1    293.09
2   foo2    0       0
3   foo3    bar3    342.3
4                   211.796

Desired Result:

0   foo     bar     total
1   foo1    bar1    293.09
2   foo2    0       0
3   foo3    bar3    342.3
4                   317.695
jezrael

As John Galt commented need '0' because 0 is string:

df = df.append(df[(df.bar != '0')].mean(numeric_only=True), ignore_index=True)
print (df)
    foo   bar    total
0  foo1  bar1  293.090
1  foo2     0    0.000
2  foo3  bar3  342.300
3   NaN   NaN  317.695

If need remove NaNs in last row only use reindex with parameter fill_value:

s = df[(df.bar != '0')].mean(numeric_only=True).reindex(df.columns, fill_value='')
df = df.append(s, ignore_index=True)
print (df)
    foo   bar    total
0  foo1  bar1  293.090
1  foo2     0    0.000
2  foo3  bar3  342.300
3              317.695

Another solution - setting with enlargement:

df.loc[len(df.index)] = s
print (df)
    foo   bar    total
0  foo1  bar1  293.090
1  foo2     0    0.000
2  foo3  bar3  342.300
3              317.695

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 DataFrame, How do I remove all columns and rows that sum to 0

From Dev

Pandas DataFrame, How do I remove all columns and rows that sum to 0

From Dev

Pandas; calculate mean and append mean to original frame

From Dev

In Pandas how can I map a cell in a filtered DataFrame to the corresponding cell in the original?

From Dev

How to calculate the mean of a pandas DataFrame with NaN values

From Dev

pandas select rows by condition for all of dataframe columns

From Dev

How do I drop rows from a Pandas dataframe based on data in multiple columns?

From Dev

How do you filter rows in a pandas dataframe conditional on columns existing?

From Java

How to append rows in a pandas dataframe in a for loop?

From Dev

How to calculate mean in a dataframe?

From Dev

pandas dataframe imported CSV with all rows in one column. How do I fix this?

From Dev

Pandas append filtered row to another DataFrame

From Dev

Convert arrays stored in a pandas columns to a new dataframe columns , append and map the resulting array values to the original dataframe

From Dev

Python pandas dataframe group mean filtered by condition

From Dev

Pandas DataFrame how to group (pivot?) rows by values of specified columns, but keeping the original index?

From Dev

pandas: How do I select rows based on the sum of all columns?

From Dev

how to switch columns rows in a pandas dataframe

From Dev

How to filter Pandas rows by another Dataframe columns?

From Dev

How to transpose dataframe columns into rows in pandas

From Dev

Python Pandas Dataframe Append Rows

From Dev

Python Pandas Dataframe Append Rows

From Dev

How do I take rows in Pandas Dataframe and transform into values for a Column?

From Dev

How do I add one row of a PANDAS dataframe to the rest of the rows?

From Dev

How do I copy rows in a pandas DataFrame and add an id column

From Java

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

From Dev

How do I split a string into several columns in a dataframe with pandas Python?

From Dev

How do I iterate through 2 columns of pandas dataframe data?

From Dev

mean of all the columns of a panda dataframe?

From Dev

How to remove rows where all numerical columns contain zero in Pandas Dataframe with mixed type of columns?

Related Related

  1. 1

    Pandas DataFrame, How do I remove all columns and rows that sum to 0

  2. 2

    Pandas DataFrame, How do I remove all columns and rows that sum to 0

  3. 3

    Pandas; calculate mean and append mean to original frame

  4. 4

    In Pandas how can I map a cell in a filtered DataFrame to the corresponding cell in the original?

  5. 5

    How to calculate the mean of a pandas DataFrame with NaN values

  6. 6

    pandas select rows by condition for all of dataframe columns

  7. 7

    How do I drop rows from a Pandas dataframe based on data in multiple columns?

  8. 8

    How do you filter rows in a pandas dataframe conditional on columns existing?

  9. 9

    How to append rows in a pandas dataframe in a for loop?

  10. 10

    How to calculate mean in a dataframe?

  11. 11

    pandas dataframe imported CSV with all rows in one column. How do I fix this?

  12. 12

    Pandas append filtered row to another DataFrame

  13. 13

    Convert arrays stored in a pandas columns to a new dataframe columns , append and map the resulting array values to the original dataframe

  14. 14

    Python pandas dataframe group mean filtered by condition

  15. 15

    Pandas DataFrame how to group (pivot?) rows by values of specified columns, but keeping the original index?

  16. 16

    pandas: How do I select rows based on the sum of all columns?

  17. 17

    how to switch columns rows in a pandas dataframe

  18. 18

    How to filter Pandas rows by another Dataframe columns?

  19. 19

    How to transpose dataframe columns into rows in pandas

  20. 20

    Python Pandas Dataframe Append Rows

  21. 21

    Python Pandas Dataframe Append Rows

  22. 22

    How do I take rows in Pandas Dataframe and transform into values for a Column?

  23. 23

    How do I add one row of a PANDAS dataframe to the rest of the rows?

  24. 24

    How do I copy rows in a pandas DataFrame and add an id column

  25. 25

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

  26. 26

    How do I split a string into several columns in a dataframe with pandas Python?

  27. 27

    How do I iterate through 2 columns of pandas dataframe data?

  28. 28

    mean of all the columns of a panda dataframe?

  29. 29

    How to remove rows where all numerical columns contain zero in Pandas Dataframe with mixed type of columns?

HotTag

Archive