Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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 将总计和计数行添加到数据帧_Python_Pandas - Fatal编程技术网

Python 将总计和计数行添加到数据帧

Python 将总计和计数行添加到数据帧,python,pandas,Python,Pandas,我有一个数据框,如下所示: dashboard = pd.DataFrame({ 'id':[1,2,3,4], 'category': ['a', 'b', 'a', 'c'], 'price': [123, 151, 21, 24], 'description': ['IT related', 'IT related', 'Marketing',''] }) 我需要添加一行,仅显示某些类别的总和和计数,如下所示: pd.DataFrame({ 'id': [3], 'categ

我有一个数据框,如下所示:

dashboard = pd.DataFrame({
 'id':[1,2,3,4],
 'category': ['a', 'b', 'a', 'c'],
 'price': [123, 151, 21, 24],
 'description': ['IT related', 'IT related', 'Marketing','']
})
我需要添加一行,仅显示某些类别的总和和计数,如下所示:

pd.DataFrame({
 'id': [3],
 'category': ['a&b'],
 'price': [295],
 'description': ['']
})

我预先计算每个类别的所有总和,然后为每一对添加总和和类别名称,并附加新行

试试这个:

将熊猫作为pd导入
dashboard=pd.DataFrame({
“id”:[1,2,3,4],
‘类别’:[‘a’、‘b’、‘a’、‘c’],
"价格":[123,151,21,24],,
'description':['IT related','IT related','Marketing','']
})
成对=[('a','b')]
groups=dashboard.groupby(“类别”)['price'].sum()
对于c1,c2成对:
新的_id=sum((仪表板['category']==c1)|(仪表板['category']==c2))
名称=“{}&{}”。格式(c1,c2)
价格总和=组[c1]+组[c2]
dashboard=dashboard.append(pd.DataFrame({'id':[new_id],'category':[name],'price':[price_sum],'description':['']}))
打印(仪表板)

使用
.agg
的选项:

dashboard=pd.DataFrame({
“id”:[1,2,3,4],
‘类别’:[‘a’、‘b’、‘a’、‘c’],
"价格":[123,151,21,24],,
'description':['IT related','IT related','Marketing','']
})
a_b=dashboard[dashboard['category'].isin(['a','b'])].agg({'id':'count','price':sum})
df=pd.DataFrame({'a&b':a_b})
屈服

a&b
id 3
价格295
然后,您可以
.transpose()
并根据需要合并到现有数据框中,或者编译一个单独的汇总结果数据框,等等。

尝试以下操作:

密码
对于每一对类别?只是具体的?只是具体的。在本例中,a&bwhy
'id':[4]
'description':['']
?您是对的。它应该是三个(类别a和b中的ID数量)。谢谢我编辑了它。关于描述,我要空白你选择分类的逻辑是什么?很难理解“仅适用于以下某些类别”的含义
dashboard = pd.DataFrame({
 'id':[1,2,3,4],
 'category': ['a', 'b', 'a', 'c'],
 'price': [123, 151, 21, 24],
 'description': ['IT related', 'IT related', 'Marketing','']
})

selection =['a','b']
selection_row = '&'.join(selection)
df2 = dashboard[dashboard['category'].isin(selection)].agg({'id' : ['count'], 'price' : ['sum']}).fillna(0).T
df2['summary'] = df2['count'].add(df2['sum'])

df2.loc['description'] =np.nan
df2.loc['category'] = selection_row

final_df = df2['summary']

final_df

id               3
price          295
description    NaN
category       a&b
Name: summary, dtype: object