Pandas Sum and count only for some columns

I just started learning pandas and this is a very simple question. Believe me, I was looking for an answer, but I can not find it.

Can you run this python code?

import pandas as pd

df = pd.DataFrame({'A':[1,0], 'B':[2,4], 'C':[4,4], 'D':[1,4],'count__4s_abc':[1,2],'sum__abc':[7,8]})

df

How to create a column "count__4s_abc" in which I want to count how many times the number 4 appears only in AC columns? (Ignoring column D.)

How to create a column "sum__abc" in which I want to sum the sums only in AC columns? (Ignoring column D.)

Thanks so much for any help!

+4
source share
2 answers

Using drop

df.assign(
    count__4s_abc=df.drop('D', 1).eq(4).sum(1),
    sum__abc=df.drop('D', 1).sum(1)
)

Or explicitly select three columns.

df.assign(
    count__4s_abc=df[['A', 'B', 'C']].eq(4).sum(1),
    sum__abc=df[['A', 'B', 'C']].sum(1)
)

Or using ilocto get the first 3 columns.

df.assign(
    count__4s_abc=df.iloc[:, :3].eq(4).sum(1),
    sum__abc=df.iloc[:, :3].sum(1)
)

All give

   A  B  C  D  count__4s_abc  sum__abc
0  1  2  4  1              1         7
1  0  4  4  4              2         8
+4
source

Another option:

In [158]: formulas = """
     ...: new_count__4s_abc = (A==4)*1 + (B==4)*1 + (C==4)*1
     ...: new_sum__abc = A + B + C
     ...: """

In [159]: df.eval(formulas)
Out[159]:
   A  B  C  D  count__4s_abc  sum__abc  new_count__4s_abc  new_sum__abc
0  1  2  4  1              1         7                  1             7
1  0  4  4  4              2         8                  2             8

DataFrame.eval() ( ) Pandas

+2

Source: https://habr.com/ru/post/1694552/


All Articles