Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在dataframe groupby中计数_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何在dataframe groupby中计数

Python 如何在dataframe groupby中计数,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个数据框架,其中包含买家和卖家的数据。我想对每一对进行分组,看看卖家和买家有多少次交易,这也是他们所有交易的总和,计算出他们交易的总花费: df2 = df.groupby(['ID of seller','ID of buyer', 'currency'])["Total spent"].sum().reset_index(drop = False) 这给了我: df2= 我想添加另一列,说明每对股票的交易次数。所以它看起来像: ID of seller I

我有一个数据框架,其中包含买家和卖家的数据。我想对每一对进行分组,看看卖家和买家有多少次交易,这也是他们所有交易的总和,计算出他们交易的总花费:

df2 = df.groupby(['ID of seller','ID of buyer', 'currency'])["Total spent"].sum().reset_index(drop = False) 
这给了我:

df2=

我想添加另一列,说明每对股票的交易次数。所以它看起来像:

ID of seller    ID of buyer    Currency   Total spent  Num of transactions
871             356            GBP        60           2
etc...

据我所知,这将使用.count()完成,但我似乎无法确定在何处安装它。

这应该可以。您应该提供一个可复制的示例和测试所需的结果

df.groupby(['ID of seller','ID of buyer', 'currency']).agg(total_spent=('Total spent', 'sum'), num_txn=('Total spent', 'count'))

这应该行得通。您应该提供一个可复制的示例和测试所需的结果

df.groupby(['ID of seller','ID of buyer', 'currency']).agg(total_spent=('Total spent', 'sum'), num_txn=('Total spent', 'count'))

这回答了你的问题吗?这回答了你的问题吗?请参阅文档。请参阅文档。