0

I have a pandas dataframe with Binary values in the columns as such.

Id Label1 Label2 ......

1    0      1  

2    1      1 

3    1      1

4    1      1

The output I am looking for is as follows

Label1

  3

Label2

  4

How do I do this using some form of code that reads each of the columns in the whole panda data-frame and provides the sum for that column. I do know how to do it for one column.

  • I do not want it for just a single column, but sorry if I have not made myself clear, how do I do it for each column at the same time. I do know how to do it for a single column. – John Smith Jul 05 '18 at 13:43
  • You can use a `for` loop.. Try it, share your code, show us where you got stuck, why you got stuck, what error you get, etc. Your desired output also makes no sense (is that a dataframe with 4 rows?). – jpp Jul 05 '18 at 13:54
  • No I have 160 rows and 4500+ columns so that is quite a huge matrix, so I just wrote a simple example here to start with. I am reading online now how to do this with a for loop. – John Smith Jul 05 '18 at 14:23
  • Please [edit your question](https://stackoverflow.com/posts/51191615/edit) with *all* your requirements, then vote to reopen. If others agree with you, it'll be reopened. An example here should include your *existing* code, not just input + output. – jpp Jul 05 '18 at 14:30
  • `for col in df.columns: s = df.groupby([col]).size() print(s)` – John Smith Jul 06 '18 at 09:31
  • I have tried the above python code and it seems to do the job! If there is a neater way then please let me know @jpp – John Smith Jul 06 '18 at 09:39

0 Answers0