通过嵌套的字典键将pandas数据框分组 [英] Group pandas dataframe by a nested dictionary key

查看:115
本文介绍了通过嵌套的字典键将pandas数据框分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pandas数据框,其中的一列是字典类型.这是一个示例数据框:

I have a pandas dataframe where one of the columns is dictionary type. This is an example dataframe:

import pandas as pd
df = pd.DataFrame({'a': [1,2,3], 
                   'b': [4,5,6], 
                   'version': [{'major': 7, 'minor':1}, 
                               {'major':8, 'minor': 5},
                               {'major':7, 'minor':2}] })

df:

   a  b                   version
0  1  4  {'minor': 1, 'major': 7}
1  2  5  {'minor': 5, 'major': 8}
2  3  6  {'minor': 2, 'major': 7}

我正在寻找一种通过那个字典键之一对数据帧进行分组的方法;在这种情况下,可以通过 version 标签中的 major 键对 df 数据框进行分组.

I am looking for a way to group the dataframe by one of that dictionary key; in this case to group the df dataframe by the major key in version label.

我尝试了几种不同的方法,从将字典密钥传递到数据框groupby函数df.groupby(['version'] ['major']),该功能自 major 起就不起作用了. strong>不是数据框标签的一部分,而是要为数据框索引分配版本,但到目前为止没有任何效果.我还试图将字典平整为数据框本身中的其他列,但这似乎有其自身的问题.

I have tried a few different stuff, from passing the dictionary key to dataframe groupby function, `df.groupby(['version']['major']), which doesn't work since major is not part of dataframe label, to assigning version to the dataframe index, but nothing works so far. I'm also trying to flatten the dictionaries as additional columns in the dataframe itself, but this seems to have its own issue.

有什么主意吗?

P.S.抱歉格式化,这是我的第一个stackoverflow问题.

P.S. Sorry about formatting, it's my first stackoverflow question.

推荐答案

选项1

Option 1

df.groupby(df.version.apply(lambda x: x['major'])).size()

version
7    2
8    1
dtype: int64


df.groupby(df.version.apply(lambda x: x['major']))[['a', 'b']].sum()

选项2

Option 2

df.groupby(df.version.apply(pd.Series).major).size()

major
7    2
8    1
dtype: int64


df.groupby(df.version.apply(pd.Series).major)[['a', 'b']].sum()

这篇关于通过嵌套的字典键将pandas数据框分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆