熊猫数据框类别总和

时间:2020-07-19 15:51:29

标签: python pandas

我有一个熊猫数据框,看起来像这样:

import pandas as pd

ticker = ['YAR.OL', 'DNB.OL', 'TSLA', 'NHY.OL', 'SBO.OL', 'STB.OL']
country = ['Norway', 'Norway', 'United States', 'Norway', 'Norway', 'Norway']
alloc = [11.822, 2.917, 0.355, 74.158, 9.673, 1.075]

dfn = pd.DataFrame(country,columns =['country'])
dfn['Allocation'] = pd.DataFrame(alloc)

enter image description here

我想总结每个国家的分配,例如: 挪威:99,645 美国:0,355

如何使用生成的df在python中执行此操作?

2 个答案:

答案 0 :(得分:3)

只需在末尾添加一行代码

dfn=dfn.groupby(['country']).sum()

一目了然

import pandas as pd

ticker = ['YAR.OL', 'DNB.OL', 'TSLA', 'NHY.OL', 'SBO.OL', 'STB.OL']
country = ['Norway', 'Norway', 'United States', 'Norway', 'Norway', 'Norway']
alloc = [11.822, 2.917, 0.355, 74.158, 9.673, 1.075]

dfn = pd.DataFrame(country,columns =['country'])
dfn['Allocation'] = pd.DataFrame(alloc)
dfn=dfn.groupby(['country']).sum()

print(dfn)

输出:

country           Allocation               
Norway             99.645
United States       0.355

答案 1 :(得分:2)

首先,必须使用pandas.DataFrame.groupby函数。 see explanations here
通过pandas.DataFrame.groupby,您可以在一组名称中执行任意操作。例如mean(),而您使用的是sum()

>>> dfn2 = dfn.groupby(['country'])
>>> dfn2.sum()

country        Allocation          
Norway             99.645
United States       0.355

您也可以在一行中完成此操作。

>>> dfn.groupby(['country']).sum()

country        Allocation
Norway             99.645
United States       0.355