2 回答

TA貢獻1827條經(jīng)驗 獲得超8個贊
通過索引器訪問行值,然后按時間和站點進行分組。
假設第二個任務在第一個任務之后,您可以在一個操作中執(zhí)行兩個
Select()
任務:
對本地分組的樣本求和,保存為
SamplesSum
.?為了總結(jié)它,您需要轉(zhuǎn)換為適當?shù)念愋停乙源?code>int為例。從最后一個分組條目中獲取最后一個溫度,將其保存為
LastTemperature
int_array
最后,創(chuàng)建兩個集合(和本地樣本分組)的交集,將其保存為MatchingValues
.?在這里,從數(shù)據(jù)框行中選擇樣本值時也不要忘記正確的轉(zhuǎn)換
我有點擔心在沒有先排序的情況下選擇最后一個溫度。最后一個將只是分組中的最后一個,不確定它是最小值還是最大值。
var int_array = new int[] { 1, 2, 3 };
var dF_out = df_in.Rows
? ? .GroupBy(row => new { Time = row[0], Site = row[1] })
? ? .Select(group => new
? ? {
? ? ? ? SamplesSum = group.Sum(row => (int)row[2]),
? ? ? ? LastTemperature = group.Last()[3],
? ? ? ? MatchingValues = int_array.Intersect(group.Select(row => (int)row[2])),
? ? });
結(jié)果dF_out集合將具有這樣的結(jié)構(gòu):
[
? ?{
? ? ? "SamplesSum":25,
? ? ? "LastTemperature":28.0,
? ? ? "MatchingValues":[
? ? ? ? ?21,
? ? ? ? ?4
? ? ? ]
? ?},
? ?{
? ? ? "SamplesSum":3,
? ? ? "LastTemperature":27.0,
? ? ? "MatchingValues":[
? ? ? ? ?3
? ? ? ]
? ?}
]

TA貢獻1796條經(jīng)驗 獲得超4個贊
我經(jīng)歷了類似的任務,所以我可以為其他讀者報告一個可能的解決方案:
using System.Linq;
using Microsoft.Data.Analysis;
// Assume that df_in is a DataFrame with columns [time], [site], [samples], and [temperature]
var df_out = df_in.AsEnumerable()
.GroupBy(row => new { Time = row.Field<DateTime>("time"), Site = row.Field<string>("site") })
.Select(g => new
{
Time = g.Key.Time,
Site = g.Key.Site,
Samples = g.Sum(row => row.Field<int>("samples")),
Temperature = g.Last().Field<float>("temperature")
})
.ToDataFrame();
然后是第二個任務,
using System.Linq;
// Assume that df_out is a DataFrame with a column [samples] and int_array is an array of integers
var filtered_df = df_out.AsEnumerable()
.Where(row => int_array.Any(i => i == row.Field<int>("samples")))
.ToDataFrame();
添加回答
舉報