Python Pandas 分组、排序并基于自定义排名分配值。

3

问题描述

pandas数据框架

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']]

目标

['Group', 'Subgroup', 'Normalized']进行groupby,然后对Max CPC进行rank。接下来,我想要将与CPC Rank相关联的Max CPC映射到基于Criterion Type和我的自定义排名确定的Type Rank{'Exact':1, 'Phrase':2, 'Broadified':3, 'Broad':4}

enter image description here

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

3个回答

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)

当我尝试您的示例时,最终Max CPC等于new CPC,这不应该发生,因为我正在尝试根据它们的CPC Rank和关键字的Criterion Type重新分配Max CPC - Jarad
@Jarad 抱歉,我无法理解您的要求。请再次清晰地解释一下,我会修改我之前的答案。 - shanmuga

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 应用于第二个,以此类推。

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

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


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)

mapping 包含一个具有非唯一值的索引。当定义 New CPC 时,map(mapping) 似乎会导致 pandas.core.index.InvalidIndexError: Reindexing only valid with uniquely valued Index objects 错误。您的函数在我的示例 df 中可以工作,但在我的更大数据集上却出现了 InvalidIndexError。此外,当我在函数之外定义 mapping,然后执行 df['New CPC'] = df['Type Rank'].map(mapping) 时,它也会引发错误。这种类型的映射只能在函数中使用吗?无论如何,我需要研究一下。感谢您提供的概念。 - Jarad
这意味着每个组的Type rank存在重复值,对吗?如果是这样,那在这种情况下应该如何映射您的值? - Nader Hisham
是的,这是可能的(虽然罕见但仍有可能)。Type Rank 数字是基于我的自定义排名 {'Exact':1, 'Phrase':2, 'Broadified':3, 'Broad':4} 而得出的。该自定义排名基于 Criterion Type 进行分类,同一组中的两行可能会被归类为 Broadified,其映射为 Type Rank 中的 3。在这几种情况下,最好的排名方式是根据 Keyword 列中的加号数量——加号越多,排名越低,加号越少,排名越高。例如:+kw +2 = Type Rank 2,+kw 2, = Type Rank 3。 - Jarad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接