python - 查找 DF 中分组值的排名 1 和排名 2

标签 python pandas dataframe

给定以下 python 数据框:

>>> import pandas
>>> df1 = pandas.DataFrame({"dish"     : ["fish", "chicken", "fish", "chicken", "chicken", "veg","veg"],
...                         "location" : ["central", "central", "north", "north", "south", "central", "north"],
...                         "sales" : [1,3,5,2,4,2,2]})
>>> total_sales = df1.groupby(by="dish").sum().reset_index().set_index(["dish"])
>>> df1["proportion_sales"] = df1.apply((lambda row: row["sales"]/total_sales.loc[row["dish"]]), axis=1)
>>> df1
      dish location  sales  proportion_sales
0     fish  central      1          0.166667
1  chicken  central      3          0.333333
2     fish    north      5          0.833333
3  chicken    north      2          0.222222
4  chicken    south      4          0.444444
5      veg  central      2          0.500000
6      veg    north      2          0.500000

我想找出每个位置排名第一和排名第二的菜肴。例如,在 central 中,chicken 排名第 1,fish 排名第 3。

如何将 dish_rank_in_location df 更新为这样?这就是我所拥有的:

      dish location  sales  proportion_sales  rank
0     fish  central      1          0.166667     1
1  chicken  central      3          0.333333     1
2     fish    north      5          0.833333     1
3  chicken    north      2          0.222222     1
4  chicken    south      4          0.444444     1
5      veg  central      2          0.500000     1
6      veg    north      2          0.500000     1

预期输出:

      dish location  sales  proportion_sales  dish_rank_in_location
0     fish  central      1          0.166667     3
1  chicken  central      3          0.333333     2
2     fish    north      5          0.833333     1
3  chicken    north      2          0.222222     3
4  chicken    south      4          0.444444     1
5      veg  central      2          0.500000     1
6      veg    north      2          0.500000     2

最佳答案

此处使用 groupby + rankascending=False

df1['dish_rank_in_location'] = df1.groupby('location')\
               .proportion_sales.rank(method='dense', ascending=False)

df1

      dish location  sales  proportion_sales  dish_rank_in_location
0     fish  central      1          0.166667                    3.0
1  chicken  central      3          0.333333                    2.0
2     fish    north      5          0.833333                    1.0
3  chicken    north      2          0.222222                    3.0
4  chicken    south      4          0.444444                    1.0
5      veg  central      2          0.500000                    1.0
6      veg    north      2          0.500000                    2.0

如果您需要整数形式的排名,您可以随时进行强制转换 -

df1['dish_rank_in_location'].astype(int)

0    3
1    2
2    1
3    3
4    1
5    1
6    2
Name: dish_rank_in_location, dtype: int64

将结果分配回来。

关于python - 查找 DF 中分组值的排名 1 和排名 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47828067/

相关文章:

python - Django 1.7 是否有自动方法将日期时间字段从手动 HTML 表单转换为模型?

python - Python 如何实现它的类型对象,即类型总是类型?

python - 如何比较 Pandas 中的两个字符串变量?

r - 如何通过循环特定列索引并使用 R 中的条件或语句来创建单独的数据框?

python - Python 中的 XPATH 语法验证器

python - 运行 setup.py 时更改包名称

python - 从数据框中的其他列创建计算列

python - 合并两列上的 DataFrame

python - 计算 Pandas 中的独特广告展示次数

python - 在 Pandas/Pyspark 中测量数据完整性并按日期分组