【发布时间】:2018-10-31 13:54:22
【问题描述】:
我有熊猫数据框
Id drove swimmed walked winPerc
0 247.3 1050 782.4 1
1 37.65 1072 119.6 0.04
2 93.73 1404 3248 1
3 95.88 1069 21.49 0.1146
4 0 1034 640.8 0
5 128.1 1000 1016 0.9368
average 100.4433333 1104.833333 971.3816667
Min 0 1000 21.49
max 247.3 1404 3248`
winPerc = 1 表示该玩家以第一名获胜,类似地 winPerc = 0 表示该玩家排在最后
print("The person who ends up winning the match usually drives {:.2f} , swims {:.2f} meters, has a walked {} meters".format(df.set_index('drove')['winPerc'].idxmax(),df.set_index('swimmed')['winPerc'].idxmax(),df.set_index('walked')['winPerc'].idxmax()))
为此我得到:-
IndexError: 元组索引超出范围
我想要的是如您在上面的数据框中看到的那样,id 为 0 和 2 的行具有 winPerc = 1 我应该得到如下响应:
The person who ends up winning the match usually drives 170.52 , swims 1227 meters, has a walked 2015.2 meters 如果有多个记录 winPerc =1 那么我应该得到相应的值
还有可能没有驾驶过的玩家 (drove = 0),并且,
赢得比赛 (winPerc = 1)
print("{} number of confident Players won without driving".format(len(df['drove'].min()['winPerc'].idxmax())))
为此我收到此错误:-
IndexError: 标量变量的索引无效。
如果万一没有列值为min()或max()或mean()的行,那么我应该取那些接近的值特殊情况。
提前致谢,如果我需要解释更多,请告诉我。 :)
【问题讨论】:
-
我将删除我之前的答案,但请更新您的问题以指定您真正想要的内容,因为您对我的回答的回复尚不清楚。为您想要做的每件事提供预期输出的示例。
-
您对我的第二个问题的回答是正确的(y)请再次分享 :) 回到第一个问题,正如您在上面的数据框中看到的那样,id 为 0 和 2 的行具有 winPerc = 1 我应该得到如下响应:
The person who ends up winning the match usually drives 170.52 , swims 1227 meters, has a walked 2015.2 meters如果有多个记录 winPerc =1 那么我应该得到相应的值 -
请用预期的输出更新您的问题,而不是添加评论:)