我有一个像这样的pandas数据结构:
>>> df
Benny Daniel Doris Eric Jack Zoe
Age 75 30 95 25 28 23
Salary 2000 9000 100000 10000 12000 20000
我想找到几个不同组的平均年龄和薪水,其中每个组都是列的子集,它们可能重叠,例如这个词典:
{'Parrot lovers': ['Doris', 'Benny'], 'Tea Drinkers': ['Doris', 'Zoe'],\
'Maintainance': ['Benny', 'Jack'], 'Coffee Drinkers': ['Benny', 'Eric'],\
'Senior Management': ['Doris', 'Zoe', 'Jack']}
如何设计一个可以执行此操作的groupby函数?
答案 0 :(得分:4)
以下是我设置问题的方法......
import StringIO
import pandas as pd
df = """index Benny Daniel Doris Eric Jack Zoe
Age 75 30 95 25 28 23
Salary 2000 9000 100000 10000 12000 20000"""
df = pd.read_csv(StringIO.StringIO(df),sep="\s+").set_index('index')
d = {'Parrot lovers': ['Doris', 'Benny'], 'Tea Drinkers': ['Doris', 'Zoe'],\
'Maintainance': ['Benny', 'Jack'], 'Coffee Drinkers': ['Benny', 'Eric'],\
'Senior Management': ['Doris', 'Zoe', 'Jack']}
对于解决方案,请使用.loc
并遍历字典...
averages = {k:df.loc[:,v].mean(axis=1) for k,v in d.iteritems()}
print pd.DataFrame(averages).T #gives the nice printout...
index Age Salary
Coffee Drinkers 50.000000 6000
Maintainance 51.500000 7000
Parrot lovers 85.000000 51000
Senior Management 48.666667 44000
Tea Drinkers 59.000000 60000
答案 1 :(得分:1)
可能有一些方法可以做到这一点,这是一条路。
转置数据,并为类别添加True / False列:
In [20]: group_map = {'Parrot lovers': ['Doris', 'Benny'],
'Tea Drinkers': ['Doris', 'Zoe'],
'Maintainance': ['Benny', 'Jack'],
'Coffee Drinkers': ['Benny', 'Eric'],
'Senior Management': ['Doris', 'Zoe', 'Jack']}
In [22]: df = df.T
In [23]: for k in group_map:
...: df[k] = df.index.isin(group_map[k])
现在,您可以按任意类别进行分组以获取方法:
In [24]: df.groupby('Parrot lovers')['Salary'].mean()
Out[24]:
Parrot lovers
False 12750
True 51000
Name: Salary, dtype: int64
或者,迭代列以获得每个类别的均值。
In [24]: means = {}
...: for k in group_map:
...: means[k] = df.groupby(k)['Salary'].mean()[True]
...: means
...:
Out[24]:
{'Coffee Drinkers': 6000,
'Maintainance': 7000,
'Parrot lovers': 51000,
'Senior Management': 44000,
'Tea Drinkers': 60000}