Python Pandas <pandas.core.groupby.DataFrameGroupBy 对象在 ...>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33440640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:07:52  来源:igfitidea点击:

Python Pandas <pandas.core.groupby.DataFrameGroupBy object at ...>

pythonpandasdataframe

提问by Lucas Mascia

I am trying to group and count the same info in a row:

我正在尝试分组并连续计算相同的信息:

#Functions

def postal_saude ():
    global df, lista_solic

    #List of solicitantes in Postal Saude
    list_sol = [lista_solic["name1"], lista_solic["name2"]]

    #filter Postal Saude Solicitantes
    df = df[(df['Cliente']==lista_clientes["6"]) 
        & (df['Nome do solicitante'].isin(list_sol))]

    #Alphabetical order
    df = df.sort_index(by=['Nome do solicitante', 'nomeCorrespondente'])

    #Grouping data of column
    grouping = df.groupby('Tipo do servi?os');

    print (grouping)


postal_saude()

When it gets to the df.groupbyit raises an error

当它到达df.groupby时会引发错误

I have tried searching this same error but I have not found a valid answer to help me fix my problem.

我试过搜索同样的错误,但我没有找到一个有效的答案来帮助我解决我的问题。

回答by Leb

Take a look at this documentation about Group By

看看这个关于Group By 的文档

Group series using mapper (dict or key function, apply given function to group, return result as series) or by a series of columns

使用映射器(字典或键函数,将给定函数应用于组,将结果作为系列返回)或按一系列列对系列进行分组

The previous is taken from here

上一篇摘自这里

Here's a quick example:

这是一个快速示例:

df = pd.DataFrame({'a':[1,1,1,2,2,2,3,3,3,3],'b':np.random.randn(10)})

df
   a         b
0  1  1.048099
1  1 -0.830804
2  1  1.007282
3  2 -0.470914
4  2  1.948448
5  2 -0.144317
6  3 -0.645503
7  3 -1.694219
8  3  0.375280
9  3 -0.065624

groups = df.groupby('a')

groups # Tells you what "df.groupby('a')" is, not an error
<pandas.core.groupby.DataFrameGroupBy object at 0x00000000097EEB38>

groups.count() # count the number of 1 present in the 'a' column
   b
a   
1  3
2  3
3  4

groups.sum() # sums the 'b' column values based on 'a' grouping

          b
a          
1  1.224577
2  1.333217
3 -2.030066

You get the idea, you can build from here using the first link I provided.

你明白了,你可以使用我提供的第一个链接从这里构建。

df_count = groups.count()

df_count
   b
a   
1  3
2  3
3  4

type(df_count) # assigning the `.count()` output to a variable create a new df
pandas.core.frame.DataFrame