Pandas: using groupby to get mean for each data category

这一生的挚爱 提交于 2019-11-28 02:05:44

Can you do a df.dtypes ? In the example below type is Int as it works fine.

    import pandas as pd

    ##group by 1 columns
    df = pd.DataFrame({' data': [4610, 4611, 4612, 4613], 'Category': [2, 2,    7, 7]})
    print df.groupby('Category'). mean()


    ##Mutiple columns to group by
    df1 = pd.DataFrame({' data': [4610, 4611, 4612, 4613], 'Category': [2,    2, 7, 7], 'Category2' : ['A','B','A','B']})
    key=['Category','Category2']
    print df1.groupby( key).mean()

 Category Category2       
 2        A           4610
          B           4611
 7        A           4612
          B           4613

As mentioned, you don't give an example of the testTime and passing_site data, but I'm guessing that they're floating rate numbers. As I'm sure you can imagine, you can't group on floating numbers. Rather, you would need to group on integers or categories of some type.

try something like:

df.groupby(['data', 'category'])['passing_site', 'testTime'].mean()

You're grouping on 'data' and 'category', and then calculating the mean for the numerical columns 'passing_site' and 'testTime'.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!