4

我有一个带有选举数据的 Deedle 系列,例如:

   "Party A", 304
   "Party B", 25 
   "Party C", 570
   ....
   "Party Y", 2
   "Party Z", 258

我想创建一个这样的新系列:

   "Party C", 570
   "Party A", 304 
   "Party Z", 258
   "Others", 145

因此,我想按原样取前 3 名,并将所有其他人的总和作为新行。做这个的最好方式是什么?

4

1 回答 1

5

我认为我们在 Deedle 中没有任何东西可以使它成为单线(多么令人失望……)。所以我能想到的最好的方法是获取前 3 方的密钥,然后Series.groupInto与返回方名称(前 3 方)或返回“其他”(其他方)的密钥选择器一起使用:

// Sample data set with a bunch of parties
let election =
 [ "Party A", 304
   "Party B", 25 
   "Party C", 570
   "Party Y", 2
   "Party Z", 258 ]
 |> series

// Sort the data by -1 times the value (descending)
let byVotes = election |> Series.sortBy (~-)
// Create a set with top 3 keys (for efficient lookup)
let top3 = byVotes |> Series.take 3 |> Series.keys |> set

// Group the series using key selector that tries to find the party in top3
// and using an aggregation function that sums the values (for one or multiple values)
byVotes |> Series.groupInto 
    (fun k v -> if top3.Contains(k) then k else "Other")
    (fun k s -> s |> Series.mapValues float |> Stats.sum)
于 2014-12-18T02:31:35.667 回答