Python Pandas groupby,rank,然后根据自定义排名分配值

时间:2015-09-05 09:39:00

标签: python dictionary pandas dataframe ranking

问题设置

pandas Dataframe

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'],然后rank Max CPC。接下来,我想将与Max CPC相关联的CPC Rank映射到基于Type Rank和我自己的自定义排名确定的Criterion Type{'Exact':1, 'Phrase':2, 'Broadified':3, 'Broad':4}

enter image description here

结果将是New CPC列及其相应的Max CPC

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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)

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

然后,我所要做的就是创建一个按Max CPC排序的Series CPC Rank

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

答案 2 :(得分: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)