【问题标题】:How to get values and also number of rows having that particular value, which is derived from multiple condtions on pandas dataframe?如何获取值以及具有该特定值的行数,该特定值源自熊猫数据帧上的多个条件?
【发布时间】: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 那么我应该得到相应的值
  • 请用预期的输出更新您的问题,而不是添加评论:)

标签: python pandas dataframe


【解决方案1】:

我复制了第一个打印件,没有做任何更改,它对我来说很好:

The person who ends up winning the match usually drives 247.30 , swims 1050.00 meters, has a walked 782.4 meters.

当您使用.format() 并获得IndexError: tuple out of range 时,这意味着您使用的变量太少。


对于第二个问题,您需要过滤您的DataFrame。这可以通过不同的方式来完成,使用布尔掩码是一种常见的方式。

>> drove_is_0 = df["drove"] == df['drove'].min()
>> is_winner =  df['winPerc'] == df['winPerc'].idxmax()

然后将您的过滤器应用到您的DataFrame:

>> filtered = df[drove_is_0 & is_winner]

最后打印:

>> print("{} number of confident Players won without driving".format(len(filtered)))
1 number of confident Players won without driving

OP 已澄清第一个问题不是关于提出的IndexError,而是关于过滤。他们想要过滤列winPerc 上的df,其中值为1,然后计算不同列的mean 值。为了保持一致性,我将使用如上所示的布尔掩码:

>> is_winner = df["winPerc"] == 1

>> mean_driven_winner = df[is_winner]["drove"].mean()
>> mean_swimmed_winner = df[is_winner]["swimmed"].mean()
>> mean_walked_winner = df[is_winner]["walked"].mean()

>> print("The person who ends up winning the match usually drives {:.2f} , swims {:.2f} meters, has a walked {} meters".format(
    mean_driven_winner, mean_swimmed_winner, mean_walked_winner)
)

The person who ends up winning the match usually drives 170.52 , swims 1227.00 meters, has a walked 2015.2 meters

【讨论】:

  • 感谢回复,如您所见,有2条获奖记录Id drove swimmed walked winPerc 0 247.3 1050 782.4 1 2 93.73 1404 3248 1我想获取winPerc为1的列的平均值
  • 在您澄清后更新了第一个问题的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-25
  • 2017-06-09
  • 2018-03-04
  • 2013-06-29
  • 1970-01-01
  • 2015-06-28
  • 2019-11-12
相关资源
最近更新 更多