按 Excel 或 Python 中的公共 ID 和值求和

Sum by common ID and value in Excel or Python

我有使用 python 和 excel 的经验,并且对两者都感到满意,所以无论哪个提供更好的解决方案,我都会使用。我有一个 CSV 文件,我已将其粘贴在此 google sheet 中,以便你们查看 (https://docs.google.com/spreadsheets/d/104nstdSWUWZa22NbCcdeymcITntev346omPhttrL1oc/edit?usp=sharing)。我想要做的是将 B 列中的团队从 M 列中找到他们最高的 5 人组合的总和,不包括 D 列中位置为“P”的任何球员。所以基本上我想要一些可以找到最高的 5 人的东西M 列中的值与 B 列中的同一团队相关联,并将它们加在一起。预先感谢您的帮助!

您可以使用一些自定义 m 代码进行组聚合,以 return 前 5 名的总和。

这是一个使用您的数据的示例:

M代码

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Team", type text}, {"Opponent", type text}, {"Position", type text}, {"DFS ID", Int64.Type}, {"SS Projection", type number}, {"My Projection", type any}, {"Actual", type any}, {"Price", Int64.Type}, {"Value", type number}, {"SS Own", type number}, {"My Own", type any}, {"dk_points", type number}, {"dk_25_percentile", type number}, {"dk_50_percentile", type number}, {"dk_75_percentile", type number}, {"dk_85_percentile", type number}, {"dk_95_percentile", type number}, {"dk_99_percentile", type number}, {"fd_points", type number}, {"fd_25_percentile", Int64.Type}, {"fd_50_percentile", type number}, {"fd_75_percentile", type number}, {"fd_85_percentile", type number}, {"fd_95_percentile", type number}, {"fd_99_percentile", type number}, {"fdraft_points", type number}, {"fdraft_25_percentile", type number}, {"fdraft_50_percentile", type number}, {"fdraft_75_percentile", type number}, {"fdraft_85_percentile", type number}, {"fdraft_95_percentile", type number}, {"fdraft_99_percentile", type number}, {"yahoo_points", type number}, {"yahoo_25_percentile", type number}, {"yahoo_50_percentile", type number}, {"yahoo_75_percentile", type number}, {"yahoo_85_percentile", type number}, {"yahoo_95_percentile", type number}, {"yahoo_99_percentile", type number}, {"dk_std", type number}, {"fd_std", type number}, {"fdraft_std", type number}, {"yahoo_std", type number}, {"BatOrder", Int64.Type}, {"PA", type number}, {"Singles", type number}, {"Doubles", type number}, {"Triples", type number}, {"HR", type number}, {"R", type number}, {"RBI", type number}, {"BB", type number}, {"SB", type number}, {"CS", type number}, {"IP", type number}, {"Wins", type number}, {"QS", type number}, {"K", type number}, {"CG", type number}, {"CGSO", type number}, {"Pitches", type number}}),
//remove position P
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Position] <> "P")),

//Group and use a custom aggregation
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Team"}, {
        {"Sum of Top 5", each List.Sum(List.MaxN([dk_points],5)), type number}
        })
in
    #"Grouped Rows"

注:以上将return前5个分数的SUM。所以得分{8,7,6,5,5,5,5,4,3} => 31。如果你想要 return 45,那将是一个不同的算法。