我正在尝试为使用Pandas的组添加过滤器。在下面的棒球数据中,我希望计算出从最初的'N'到最后一个'Y'所花费的平均时间。本质上,我希望计算每个组的长度,这些组在归纳列中包含一个“Y”,并且有多个行。任何提示都会有帮助!在
playerID yearid votedBy ballots needed votes inducted category needed_note
2860 aaronha01 1982 BBWAA 415 312 406 Y Player NaN
3743 abbotji01 2005 BBWAA 516 387 13 N Player NaN
146 adamsba01 1937 BBWAA 201 151 8 N Player NaN
259 adamsba01 1938 BBWAA 262 197 11 N Player NaN
384 adamsba01 1939 BBWAA 274 206 11 N Player NaN
497 adamsba01 1942 BBWAA 233 175 11 N Player NaN
574 adamsba01 1945 BBWAA 247 186 7 N Player NaN
2108 adamsbo03 1966 BBWAA 302 227 1 N Player NaN
我修改了你的数据集,这样就有两个这样的组。一个有2行从}。这取决于您是否在
N
到Y
,另一个有8行从N
到{y
包含行中计数。否则,它将有两个组,一个包含1行,另一个包含7行。看起来你没有时间序列列,所以我想这意味着这些行在时间上是均匀分布的。在假设不计算
^{pr2}$Y
,则平均值可以通过以下公式计算:类DataFrameGroupBy的过滤器方法对组中的每个子帧进行操作。请参见
help(pd.core.groupby.DataFrameGroupBy.filter)
。数据如下:示例代码:
^{pr2}$我模拟了我自己的数据,为你的问题做了一个简单的测试。我创建了一组名为df_inducted的玩家,其中包括最终加入的玩家,通过使用isin()函数,我们可以确保在分析中只考虑他们。然后我求出他们约会的最小值和最大值,并求出它们的平均值。在
相关问题 更多 >
编程相关推荐