2015-09-05 115 views
2

问题设置Python的熊猫GROUPBY,排名,然后根据自定义排名

大熊猫据帧

df = pd.DataFrame({'Group': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A'], 'Subgroup': ['Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 2', 'Group 2', 'Group 2'], 'Keyword': ['kw 1', 'kw 1', 'kw 1', 'kw 2', '+kw +2', 'kw 2', 'kw 3', 'kw 3', 'kw 3'], 'Normalized': ['kw 1', 'kw 1', 'kw 1', 'kw 2', 'kw 2', 'kw 2', 'kw 3', 'kw 3', 'kw 3'], 'Criterion Type': ['Exact', 'Phrase', 'Broad', 'Phrase', 'Broadified', 'Exact', 'Broad', 'Exact', 'Phrase'], 'Max CPC': [1.62, 1.73, 0.87, 1.70, 0.85, 1.60, 0.99, 1.58, 1.68], 'CPC Rank': [2, 1, 3, 1, 3, 2, 3, 2, 1], 'Type Rank': [1, 2, 3, 2, 3, 1, 3, 1, 2]}) 

该值分配给获得在正确的位置的列:

df = df[['Group', 'Subgroup', 'Keyword', 'Normalized', 'Criterion Type', 'Max CPC', 'CPC Rank', 'Type Rank']] 

目标

groupby['Group', 'Subgroup', 'Normalized'],然后rankMax CPC s。接下来,我要地图关联到CPC RankType Rank这是基于Criterion Type确定,我自己的自定义排名的Max CPC{'Exact':1, 'Phrase':2, 'Broadified':3, 'Broad':4}

enter image description here

的结果将是New CPC列其适当Max CPC

回答

0
import pandas as pd 
import numpy as np 

df = pd.DataFrame({'Group': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A'], 'Subgroup': ['Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 1', 'Group 2', 'Group 2', 'Group 2'], 'Keyword': ['kw 1', 'kw 1', 'kw 1', 'kw 2', '+kw +2', 'kw 2', 'kw 3', 'kw 3', 'kw 3'], 'Normalized': ['kw 1', 'kw 1', 'kw 1', 'kw 2', 'kw 2', 'kw 2', 'kw 3', 'kw 3', 'kw 3'], 'Criterion Type': ['Exact', 'Phrase', 'Broad', 'Phrase', 'Broadified', 'Exact', 'Broad', 'Exact', 'Phrase'], 'Max CPC': [1.62, 1.73, 0.87, 1.70, 0.85, 1.60, 0.99, 1.58, 1.68], 'CPC Rank': [2, 1, 3, 1, 3, 2, 3, 2, 1], 'Type Rank': [1, 2, 3, 2, 3, 1, 3, 1, 2]}) 
df = df[['Group', 'Subgroup', 'Keyword', 'Normalized', 'Criterion Type', 'Max CPC', 'CPC Rank', 'Type Rank']] 

#Sort by custom priority based on their Criterion Type 
df = df.sort(['Group', 'Subgroup', 'Normalized', 'Type Rank']) 
#Reset index and drop old one 
df = df.reset_index(drop=True) 
print(df) 
#Create df1 which is a Series of the Max CPC column in its correctly ranked order 
df1 = df.sort(['Group', 'Subgroup', 'Normalized', 'CPC Rank'])['Max CPC'] 
#Reset index and drop old one 
df1 = df1.reset_index(drop=True) 
print(df1) 

#Add the df1 Series to df and name the column New CPC 
df['New CPC'] = df1 

print(df) 

这是迄今为止最有效的解决了这个问题。困难的部分是意识到我可以通过Type Ranksortdf行,所以Criterion Type行按排名排序。这意味着我希望最高的Max CPC适用于第一个,第二个最高的Max CPC适用于第二个,依此类推。

然后我所要做的就是创建一个Max CPCSeries排序方式CPC Rank

最后,将此Series添加到现有的df

0

我已经对每个组内的值进行了排序并使用索引分配了排序后的值。 这是你想要的吗?

df['new CPC'] = -1 
parts = [] 
grouped = df.groupby(['Group', 'Subgroup', 'Normalized']) 
for name, group in grouped: 
    type_rank_index = group.sort(columns='Type Rank').index 
    cpc_rank_index = group.sort(columns='CPC Rank').index 
    group.loc[type_rank_index, 'new CPC'] = group.loc[cpc_rank_index, 'Max CPC'] 
    parts.append(group) 

result = pd.concat(parts) 
+0

当我尝试你的榜样,最终,'最大CPC'等于'新CPC'这不应该发生,因为我想为基础,以重新分配'最大CPC's他们的“CPC排名”和关键字的“标准类型”。 – Jarad

+0

@Jarad抱歉,我无法理解您的要求。请再次清楚解释,我会在上面更改我的答案。 – shanmuga

0

试试这个

def group_rank(df): 
    # first of all you've to rank according to `Max CPC` 
    df['CPC Rank'] = df['Max CPC'].rank(ascending = False) 
    # create the mapping 
    mapping = pd.Series(data=df['Max CPC'].values , index= df['CPC Rank'].values) 
    # create new column according to your ranking 
    df['New CPC'] = df['Type Rank'].map(mapping) 
    return df 

df.groupby(['Group', 'Subgroup', 'Normalized']).apply(group_rank) 
+0

'mapping'包含一个非唯一值的索引。在定义'New CPC'时,'map(映射)'似乎会导致'pandas.core.index.InvalidIndexError:Reindexing只对唯一赋值的索引对象有效。你的函数在我的例子'df'中工作,但是我在我的大数据集上得到了'InvalidIndexError'。另外,当我把'mapping'定义为不在函数中时,那么执行'df ['New CPC'] = df ['Type Rank'] .map(mapping)',它也会引发错误。这种类型的映射只能在一个函数中工作吗?无论哪种方式,我都需要研究这一点。感谢这个概念。 – Jarad

+0

这意味着您有每个组重复的Type排名值,是吗?如果是这样的话,你应该如何映射你的值? –

+0

是的,这是一种可能性(罕见/不常见,但仍有可能)。 'Type Rank'数字是根据我自己的习惯级别推导出来的:'{'Exact':1,'Phrase':2,'Broadified':3,'Broad':4}'。这个自定义的等级是基于'Criterion Type'的,并且同一个组中的两行可能是'Broadified',它映射到'3'作为'Type Rank'。在这些少数情况下,排名的最佳方式是基于“关键字”列中的+符号 - 更多+符号,排名越低,符号越少,排名越高。例如:+ kw +2 ='类型等级'2,+ kw 2,='类型等级'3。 – Jarad