0

DataFrame of 3 Column

a   b   c               
1   2   4               
1   2   4               
1   2   4               

Want Output like this

a   b   c   a+b a+c b+c a+b+c
1   2   4   3   5   6   7
1   2   4   3   5   6   7
1   2   4   3   5   6   7
stellasia
  • 5,372
  • 4
  • 23
  • 43
  • 2
    • Welcome to Stack Overflow! Please, provide more information on what you have tried, and why it didn't work. Demonstration of the effort and a [minimal code and data to reproduce the issue](https://stackoverflow.com/help/minimal-reproducible-example) is required for the question to be accepted. Take a minute to read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – sophros Feb 13 '20 at 06:44
  • `pd.concat([df,pd.DataFrame([pd.np.array(j).sum(0) for i in range(2,4) for j in combn(df.T.values.tolist(),i)]).T],1)` – Onyambu Feb 13 '20 at 07:30
  • NameError: name 'combn' is not defined Showing – Tushar Bhonsle Feb 13 '20 at 17:24

2 Answers2

3

Create all combinations with length 2 or more by columns and then assign sum:

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)))

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  1  2  4    3    5    6      7
2  1  2  4    3    5    6      7
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Why cant `combinations` take a function as an argument?. They should update it, the same way `R's` combination takes in a function as an argument. in eg we should be able to do`combinations([1,2,3,4], 2, sum)==> [3,4,5,5,6,7]` – Onyambu Feb 13 '20 at 07:15
  • @Onyambu - I think because there are only 2 input parameters, check [this](https://docs.python.org/3/library/itertools.html#itertools.combinations) – jezrael Feb 13 '20 at 07:17
  • yeah was checking that and wondering why they cannot update it to take in another argument – Onyambu Feb 13 '20 at 07:27
  • Can we count frequency of 1 in column 1, frequency of 2 in column 2 and frequency of 4 in column 3 and on..... – Tushar Bhonsle Feb 13 '20 at 17:42
  • 1
    @jezrael- Thanks, i was not aware about "accept". Ok, i am creating new question please help me to solve. – Tushar Bhonsle Feb 14 '20 at 12:46
0

You should always post your approach while asking a question. However, here it goes. This the easiest but probably not the most elegant way to solve it. For a more elegant approach, you should follow jezrael's answer.

Make your pandas dataframe here:

import pandas as pd
df = pd.DataFrame({"a": [1, 1, 1], "b": [2, 2, 2], "c": [4, 4, 4]})

Now make your desired dataframe like this:

df["a+b"] = df["a"] + df["b"]
df["a+c"] = df["a"] + df["c"]
df["b+c"] = df["b"] + df["c"]
df["a" + "b" + "c"] = df["a"] + df["b"] + df["c"]

This gives you:

|    |   a |   b |   c |   a+b |   a+c |   b+c |   abc |
|---:|----:|----:|----:|------:|------:|------:|------:|
|  0 |   1 |   2 |   4 |     3 |     5 |     6 |     7 |
|  1 |   1 |   2 |   4 |     3 |     5 |     6 |     7 |
|  2 |   1 |   2 |   4 |     3 |     5 |     6 |     7 |
Redowan Delowar
  • 1,580
  • 1
  • 14
  • 36