Python 大熊猫<;pandas.core.groupby.DataFrameGroupBy对象位于…>;
我正在尝试将相同的信息分组并计数成一行:Python 大熊猫<;pandas.core.groupby.DataFrameGroupBy对象位于…>;,python,pandas,dataframe,Python,Pandas,Dataframe,我正在尝试将相同的信息分组并计数成一行: #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']=
#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()
当它到达df.groupby时,会引发一个错误
我已尝试搜索同一错误,但没有找到有效的答案来帮助我解决问题。请查看有关的文档 使用映射器对系列进行分组(dict或键函数,应用给定函数 若要进行分组,请将结果作为(系列)或按一系列列返回 前文摘自 下面是一个简单的例子:
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
这不是错误,只是groupby对象的表示。只需对对象应用聚合操作即可返回数据帧或序列。groupby上的文档中有更多关于此的信息。我可能使用了错误的命令。。。我想简单地将所有多行合并成一个combinaall“foo”。我想我可以用groupby。。。。但是我返回给我的消息需要理解的重要一点是,
groupby
对象只是一个包含有关如何执行groupby
的元数据的对象,您必须对groupby
对象执行一些操作,例如某种形式的聚合,以便返回dataframe
或series
哪种错误?可能是
?太棒了!!!非常感谢您的解释,带有注释的示例真的很有帮助!有没有办法从groupby中保存count()以便以后使用?这正是我试图实现的。为了保存数据,pickle可以很容易地使用。但在这种情况下,我建议使用类似于df\u count.to\u csv(“ds\u count.csv”,index=False)
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