I have the following dataframe in pandas where there's a unique index (employee) for each row and also a group label type:
df = pandas.DataFrame({"employee": ["a", "b", "c", "d"], "type": ["X", "Y", "Y", "Y"], "value": [10,20,30,40]})
df = df.set_index("employee")
I want to group the employees by type and then calculate a statistic for each type. How can I do this and get a final dataframe which is type x statistic, for example type x (mean of types)? I tried using groupby:
g = df.groupby(lambda x: df.ix[x]["type"])
result = g.mean()
this is inefficient since it references the index ix of df for each row - is there a better way?
g = df.groupby("type")?