我想将熊猫中的列分组

时间:2018-10-31 15:59:52

标签: python pandas dataframe group-by

我有包含不同列的数据框,并且我想以AccountID为基础对列进行分组  in the bases of AccountID

和预期的输出看起来像这样 enter image description here

2 个答案:

答案 0 :(得分:1)

尝试一下:

uniq_accounts = differenc['AccountID'].unique()
grped = differenc.groupby('AccountID')

## You can get the grouped data for a certain AccountId like this and store in a dictionary:

d = {}
for account in uniq_accounts:
    d[account] = grped.get_group(account)

##Now, dictionary has all accounts data in separate dataframes. You can access like this:

for key in d.keys():
    print(d[key])

## If you want to fetch data for a particular AccountID(lets say 316001718201), you can do:

print(d['316001718201'])

让我知道是否有帮助。

答案 1 :(得分:0)

使用DataFrame的groupby函数应该可以实现,在这种情况下,该函数可能为differenc.groupby('AccountID')