1

dataframe is like

1  2  4  ....n

0  2  0  ....n

1  0  4  ....n

0  0  4  ....n

now i want to count 1 from column 1, count 2 from column 2,count 4 from column 3 and so on.

but i also want to count few values by adding columns like

1+2  ,        1+4     ,    2+4   ,   1+2+4

0+2  ,        0+0     ,    2+0   ,   0+2+0

1+0  ,         1+4    ,     0+4  ,    1+0+4

0+0  ,         0+4    ,     0+4  ,    0+0+4

Count 3 count 5 count 6 count 7 from above columns respectively.

Sotr these values/number in list, array or dataframe like

Values/Number -- Frequency

1 , 2

2 , 2

3 , 1

4 , 3

5 , 2

6 , 1

7 , 1

webprogrammer
  • 2,393
  • 3
  • 21
  • 27

1 Answers1

0

I believe you need:

from itertools import chain, combinations
#https://stackoverflow.com/a/5898031
comb = chain(*map(lambda x: combinations(df.columns, x), range(2, len(df.columns)+1)))

cols = df.columns
for c in comb:
    df[f'{"+".join(c)}'] = df.loc[:, c].sum(axis=1)
print (df)
   a  b  c  a+b  a+c  b+c  a+b+c
0  1  2  4    3    5    6      7
1  0  2  0    2    0    2      2
2  1  0  4    1    5    4      5
3  0  0  4    0    4    4      4

Then is possible count each column, values in DataFrame are counts and index values are counted numbers:

df1 = df.apply(pd.value_counts)
print (df1)
     a    b    c  a+b  a+c  b+c  a+b+c
0  2.0  2.0  1.0  1.0  1.0  NaN    NaN
1  2.0  NaN  NaN  1.0  NaN  NaN    NaN
2  NaN  2.0  NaN  1.0  NaN  1.0    1.0
3  NaN  NaN  NaN  1.0  NaN  NaN    NaN
4  NaN  NaN  3.0  NaN  1.0  2.0    1.0
5  NaN  NaN  NaN  NaN  2.0  NaN    1.0
6  NaN  NaN  NaN  NaN  NaN  1.0    NaN
7  NaN  NaN  NaN  NaN  NaN  NaN    1.0

And if want maximum counts per rows use:

df2 = df1.max(axis=1).astype(int).rename_axis('Num').reset_index(name='count')
print (df2)
   Num  count
0    0      2
1    1      2
2    2      2
3    3      1
4    4      3
5    5      2
6    6      1
7    7      1
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252