【发布时间】:2021-06-13 22:19:54
【问题描述】:
希望有人能帮我解决这个问题。
让我们考虑以下数据示例:
dfexample = pd.DataFrame(np.array([['apple','red','a',100],
['apple','red','b',100],
['apple','red','c',80],
['apple','red','d',70],
['apple','red','e',60],
['apple','red','f',50],
['apple','yellow','a',99],
['apple','yellow','b',98],
['apple','yellow','c',97],
['apple','yellow','d',96],
['apple','yellow','e',95],
['apple','yellow','f',94],
['apple','green','a',10],
['apple','green','b',9],
['apple','green','c',8],
['apple','green','d',7],
['apple','green','e',6],
['apple','green','f',5]
]),
columns=['fruit','colour','cat','score'])
dfexample = dfexample.astype({"score": float})
我可以旋转数据以将 cat 转换为唯一列:
pivotexample = dfexample.pivot_table(index = ['fruit','colour'],
columns = ['cat'],
values = ['score'],
fill_value = 0).swaplevel(axis=1).sort_index(1)
print(pivotexample)
输出:
cat a b c d e f
score score score score score score
fruit colour
apple green 10 9 8 7 6 5
red 100 100 80 70 60 50
yellow 99 98 97 96 95 94
是否可以迭代地计算每个 cat 列的最大值(在 cat 列 a -> f 上循环),以便返回带有 fruit_colour 索引的最大值,而不是循环中较早选择的索引。
从上面的例子我想返回:
猫,(最大)分数,fruit_colour
a, 100, apple_red(最大值)
b, 98, apple_yellow(最大值为 98,因为 100(apple_red) 被选为之前的最高索引)
c, 8, apple_green(8 最大值,因为 97(apple_yellow) 和 80(apple_red) 之前的最高索引)
手动 Excel 视图:
非常感谢您的关注。
问候
############################################## ######################
编辑后提供的答案:
def calc_mystats(dx, picked_colors_):
dx = dx.sort_values(by=['score'], ascending=False)
for index, row in dx.iterrows():
if row['colour'] not in picked_colors_:
picked_colors_.append(row['colour'])
return pd.Series([row['cat'], row['score'], row['fruit'] + "_" + row['colour']], index=['cat','(max)score', 'fruit_color'])
picked_colors = ['none']
print(dfexample.groupby('cat').apply(calc_mystats, picked_colors))
print(picked_colors)
输出:
cat (max)score fruit_color
cat
a a 99.0 apple_yellow
b b 9.0 apple_green
c NaN NaN NaN
d NaN NaN NaN
e NaN NaN NaN
f NaN NaN NaN
['none', 'red', 'yellow', 'green']
【问题讨论】:
标签: python pandas numpy loops pivot-table