Advertisement
furas

Python - pandas - sum

May 20th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.44 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. data = [[1,2,3], [4,5,6], [7,8,9]]
  4.  
  5. # create table DataFrame with data
  6. df = pd.DataFrame(data)
  7.  
  8. # sum in rows and put results in new column "sum"
  9. df['sum'] = df.sum(axis=1)
  10.  
  11. # display all
  12. print(df)
  13.  
  14. '''
  15.    0  1  2  sum
  16. 0  1  2  3    6
  17. 1  4  5  6   15
  18. 2  7  8  9   24
  19. '''
  20.  
  21. data = [[1,2,3], [4,5,6], [7,8,9]]
  22.  
  23. for row in data:
  24.     print(row, sum(row))
  25.  
  26. '''
  27. [1, 2, 3] 6
  28. [4, 5, 6] 15
  29. [7, 8, 9] 24
  30. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement