如何按F#中的值按行分组? [英] How to group by values in a row in F#?
问题描述
如果我已上传CSV文件,并且将其拆分为SEQUENCE中的行.
If I have uploaded a CSV file and I have it split up into rows within a SEQUENCE.
如果说ROW 1中也存在一个值的多个实例,如何用ROW 1中的值对ROW 2中的值求平均,所以现在我在ROW 1中只有每个值的一个实例.>{这只是一个例子,ROW 1和ROW 2是理论上的.}
If there are also multiple instances of one value in lets say ROW 1, how do I average the values in ROW 2 by the values in ROW 1, so I now only have one instance of each value in ROW 1.
{This is just an example, and ROW 1 and ROW 2 are theoretical.}
请注意,我正在处理序列.
Be aware that I am working with a sequence.
数据示例和理想结果如下:
Example of data and ideal result is below:
给出了什么:
Row 1 --- Row 2 (Dollars)
2010 --- 50000.198
2010 --- 45151.451
2011 --- 75641.372
2011 --- 91652.710
2012 --- 11281.450
2012 --- 70046.154
2012 --- 97778.054
2013 --- 555574.501
2013 --- 78921.215
我想要什么:
Row 1 --- Row 2
2010 --- 47575.825
2011 --- 93647.041
2012 --- 59701.886
2013 --- 317247.858
推荐答案
听起来您已经解析了CSV文件并将值拉入序列中.对于此示例,假设您将其拉入元组列表,其中以年为第一个元素,成本为第二个元素,等效于此:
It sounds like you've already parsed the CSV file and pulled values into a sequence. For this example, let's assume you pulled it into a list of tuples with the year as the first element and the cost as the second, equivalent to this:
let costByYear =
[
(2010,50000.198)
(2010,45151.451)
(2011,75641.372)
(2011,91652.710)
(2012,11281.450)
(2012,70046.154)
(2012,97778.054)
(2013,555574.501)
(2013,78921.215)
]
您可以使用一些 Seq
函数按年份分组( Seq.average
):
You could use a few Seq
functions to group by the year (Seq.groupBy
) and then average the cost (Seq.average
):
let avgCostPerYear =
let avg (year, costs) = (year, Seq.average <| Seq.map snd costs)
Seq.groupBy fst >> Seq.map avg
运行此:
printfn "%A" (avgCostPerYear costByYear)
产量:
seq
[(2010, 47575.8245); (2011, 83647.041); (2012, 59701.886); (2013, 317247.858)]
这篇关于如何按F#中的值按行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!