In [1]:
# Import libraries
import pandas as pd
import sys
In [2]:
print 'Python version ' + sys.version
print 'Pandas version: ' + pd.__version__
In [3]:
# Our small data set
d = {'one':[1,1,1,1,1],
'two':[2,2,2,2,2],
'letter':['a','a','b','b','c']}
# Create dataframe
df = pd.DataFrame(d)
df
Out[3]:
In [4]:
# Create group object
one = df.groupby('letter')
# Apply sum function
one.sum()
Out[4]:
In [5]:
letterone = df.groupby(['letter','one']).sum()
letterone
Out[5]:
In [6]:
letterone.index
Out[6]:
You may want to not have the columns you are grouping by become your index, this can be easily achieved as shown below.
In [7]:
letterone = df.groupby(['letter','one'], as_index=False).sum()
letterone
Out[7]:
In [8]:
letterone.index
Out[8]:
Author: David Rojas