简体   繁体   中英

grouping dataframes in pandas efficiently?

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?

Like @sza says, you can use:

In [11]: g = df.groupby("type")

In [12]: g.mean()
Out[12]:
      value
type
X        10
Y        30

see the groupby docs for more...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM