9951 explained code solutions for 126 technologies


python-pandasHow to find dataframe sum for two columns


import pandas as pd

data = pd.DataFrame({
  'Vendor': ['US', 'US', 'US', 'KR', 'KR'],
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Price': [204, 304, 102, 405, 350],
  'Used': [150, 250, 80, 320, 280]
})

res = data[['Price', 'Used']].sum()ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

['Price', 'Used']

list of columns to get mean for

.sum()

calculate sum for given dataframe


Usage example

import pandas as pd

data = pd.DataFrame({
  'Vendor': ['US', 'US', 'US', 'KR', 'KR'],
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Price': [204, 304, 102, 405, 350],
  'Used': [150, 250, 80, 320, 280]
})

res = data[['Price', 'Used']].sum()
print(res)
output
Price    1365
Used     1080
dtype: int64