我经常遇到一个问题,我想在我的数据透视表中的值区域中使用的公式与我想用于该行的"总计"列的公式不同.我通常想要求值,但我想平均和.如果我在列标签,行标签上的肉类型和值中的总和订单上轮换日期,通常会得到以下内容.
Row Lables | Day 1 | Day 2 | Day 3 | Grand Total
________________________________________________
Beef | 100 | 105 | 102 | 307
Chicken | 200 | 201 | 202 | 603
Run Code Online (Sandbox Code Playgroud)
我按天收到总和,以及Grand Total列中所有日期的总和.这是我想要的:
Row Lables | Day 1 | Day 2 | Day 3 | Grand Total (Avg of Day Totals)
________________________________________________
Beef | 100 | 105 | 102 | 102.3
Chicken | 200 | 201 | 202 | 201.0
Run Code Online (Sandbox Code Playgroud)
在这种情况下,订单仍然按天汇总,但总计现在是总和的平均值.我现在所做的是将Pivot数据复制并粘贴到单独的工作表上,然后计算平均值.如果有一种方法可以使用自定义的Grand Total列进行此操作,那将是不可思议的.这对我来说是数据透视表的最大缺点之一,但我希望这是由于我的无知,这通常是.谢谢您的帮助!
我经常需要将数据帧转换为列表列表.其中一个关键是我需要保留类型(即:数字保持数字,字符串保持字符串).这是我目前使用的功能:
dataframe_to_lists <- function(df){
return_list <- lapply(split(df, 1:nrow(df)), function(row) as.list(row))
return(return_list)
}
Run Code Online (Sandbox Code Playgroud)
这是准确的,但当行数变大(> 10K)时速度不快.R中最快的方法是什么?
这是一个例子:
> example_df <- data.frame(col1 = c('a', 'b', 'c'), col2 = c(1, 2, 3), col3 = c(4, 5, 6), stringsAsFactors = FALSE)
> list_result <- dataframe_to_lists(example_df)
> list_result
$`1`
$`1`$col1
[1] "a"
$`1`$col2
[1] 1
$`1`$col3
[1] 4
$`2`
$`2`$col1
[1] "b"
$`2`$col2
[1] 2
$`2`$col3
[1] 5
$`3`
$`3`$col1
[1] "c"
$`3`$col2
[1] 3
$`3`$col3
[1] 6
Run Code Online (Sandbox Code Playgroud) 好吧,奇怪的问题在这里。我正在使用FSharp.Data.SqlClient从我们的数据库中获取记录。它推断出的记录有几个字段,它们是选项类型。我需要过滤掉任何选项类型都为 None 的记录,并在已知字段的地方创建新记录。下面是我正在谈论的一个例子。为了解决这个问题,我创建了一个过滤器函数,recordFilter在所有类型都Option<'T>包含值的情况下返回我想要的类型,None当它们不包含时返回我想要的类型。
我的问题是是否有可能创建一个函数来自动检查Option<'T>记录中的所有字段是否有值。我猜这需要某种反射来遍历记录的字段。我猜这是不可能的,但我想把它扔掉,以防万一我错了。
如果这种方法是惯用的方式,那么我会很高兴听到这个。我只是想确保我不会错过一些更优雅的解决方案。F# 的可能性始终让我感到惊讶。
我的动机是我正在处理几十个字段的记录,这些字段的类型为Option<'T>. match...with像我在这个例子中所做的那样,不得不写出大量的语句是很烦人的。只有几个字段的时候还好,30+个字段的时候就烦了。
type OptionRecord = {
Id: int
Attr1: int option
Attr2: int option
Attr3: int option
Attr4: int option
Attr5: int option
Attr6: int option
}
type FilteredRecord = {
Id: int
Attr1: int
Attr2: int
Attr3: int
Attr4: int
Attr5: int
Attr6: int
}
let optionRecords = [for i in 1..5 ->
{
OptionRecord.Id = i
Attr1 = Some …Run Code Online (Sandbox Code Playgroud) 这似乎应该是一个常见的用例,但我没有找到任何好的指导.我有一个有效的解决方案,但我宁愿使用矢量化查找而不是使用Pandas apply()函数.
这是我正在做的一个例子:
import pandas as pd
example_dict = {
"category1":{
"field1": 0.0,
"filed2": 5.0},
"category2":{
"field1": 5.0,
"field2": 8.0}}
d = {"ids": range(10),
"category": ["category1" if x % 2 == 0 else "category2" for x in range(10)]}
df = pd.DataFrame(d)
# The operation I am trying to vectorize
df['category_data'] = df.apply(lambda row: example_dict[row['category']], axis=1)
Run Code Online (Sandbox Code Playgroud)
在最后一行,您可以看到我使用该apply()函数执行字典查找的位置.我的直觉告诉我应该有一种方法来矢量化这个.我可能错了,但我也想知道.我经常遇到需要在字典中查找信息并将其添加为a列的情况DataFrame.
我正在创建一个用于建模的 DSL,并且我希望能够创建一个Settings具有两个自定义操作的构建器:Buffer和Constraint,它们本身就是计算表达式。原因是该域中的术语严重过载,而计算表达式允许您通过使用自定义操作来提供上下文。
我不知道如何让这种嵌套按预期工作。我在代码示例的底部提供了一个示例,说明了我想要的结果。
type Buffer =
{
Name : string
Capacity : float
}
type Constraint =
{
Name : string
Limit : float
}
[<RequireQualifiedAccess>]
type Setting =
| Buffer of Buffer
| Constraint of Constraint
type BufferBuilder (name: string) =
member _.Yield _ : Buffer = { Name = name; Capacity = 0.0 }
member _.Run x : Buffer = x
[<CustomOperation("Capacity")>]
member _.Capacity (b: Buffer, newCapacity) =
{ b with Capacity …Run Code Online (Sandbox Code Playgroud) I am trying to tackle the scariest part of programming for me and that is parsing and ASTs. I am working on a trivial example using F# and FParsec. I am wanting to parse a simple series of multiplications. I am only getting the first term back though. Here is what I have so far:
open FParsec
let test p str =
match run p str with
| Success(result, _, _) -> printfn "Success: %A" result
| Failure(errorMsg, _, _) …Run Code Online (Sandbox Code Playgroud) 我想以特定的方式格式化元组,并且我尝试通过检查元组的类型(2 个元素、3 个元素等)来做到这一点。我在第三行收到错误消息:
This runtime coercion of type test from type
'd
to
'a * ('b * 'c)
involves an indeterminate type based on the information prior to this program point.
Runtime type tests are not allowed on some type. Further type annotations are needed.
Run Code Online (Sandbox Code Playgroud)
这是我的尝试:
This runtime coercion of type test from type
'd
to
'a * ('b * 'c)
involves an indeterminate type based on the information prior to this program point.
Runtime type tests are not allowed …Run Code Online (Sandbox Code Playgroud) 我有一个用 F# for .NET 编写的使用 SSE2 的函数。我使用 AVX2 写了同样的东西,但根本问题是相同的。a 的预期目的是什么MoveMask?我知道它对我的目的有效,我想知道为什么。
我正在迭代两个 64 位浮点数组a和b,测试它们的所有值是否匹配。我正在使用该CompareEqual方法(我相信该方法正在包装对 的调用__m128d _mm_cmpeq_pd)一次比较多个值。然后我将该结果与64Vector128位0.0浮点数进行比较。我的推理是,在值不匹配的情况下,结果CompareEqual将给出一个值。0.0到目前为止,这是有道理的。
然后我Sse2.MoveMask对与零向量的比较结果使用该方法。我之前曾研究过使用SSEand for 匹配,并且我看到了人们用于测试非零值AVX的示例。MoveMask我相信这种方法正在使用int _mm_movemask_epi8英特尔内在函数。我已包含 F# 代码和 JIT 的程序集。
这真的是一个目的吗MoveMask,还是只是一个令人高兴的巧合,它可以达到这些目的。我知道我的代码有效,我想知道它为什么有效。
#nowarn "9" "51" "20" // Don't want warnings about pointers
open System
open FSharp.NativeInterop
open …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用计算表达式来构建操作列表。我需要绑定到从getFood操作返回的值,以便我可以注册稍后的步骤来使用它。
type Food =
| Chicken
| Rice
type Step =
| GetFood of Food
| Eat of Food
| Sleep of duration:int
type Plan = Plan of Step list
type PlanBuilder () =
member this.Bind (plan:Plan, f) =
f plan
member this.Yield _ = Plan []
member this.Run (Plan x) = Plan (List.rev x)
[<CustomOperation("eat")>]
member this.Eat (Plan p, food) =
printfn "Eat"
Plan ((Eat food)::p)
[<CustomOperation("sleep")>]
member this.Sleep (Plan p, duration) =
printfn "Sleep"
Plan ((Sleep …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 FsCheck 进行基于属性的测试,但我似乎无法弄清楚如何让 FsCheck 使用我已注册的基因。下面是为域生成类型的代码:
module Flips.Gens
open Flips.Domain
open FsCheck
type Silly = {
Name : string
}
let SillyGen () =
gen {
let! NonEmptyString name = Arb.generate<NonEmptyString>
return { Name = name}
}
type Domain () =
static member ArbSillyGen () = Arb.fromGen (SillyGen ())
Run Code Online (Sandbox Code Playgroud)
这是示例测试
module Flips.Tests
open Xunit
open FsCheck
open FsCheck.Xunit
open Flips.Gens
do Arb.register<Domain> () |> ignore
module Tests =
[<Property>]
let ``Silly Name is NonEmptyString`` (silly:Silly) =
let isNullOrEmpty = System.String.IsNullOrEmpty silly.Name …Run Code Online (Sandbox Code Playgroud) f# ×7
.net-core ×1
avx ×1
dictionary ×1
excel ×1
excel-2010 ×1
f#-data ×1
fparsec ×1
fscheck ×1
monads ×1
pandas ×1
parsing ×1
pivot-table ×1
powerpivot ×1
python ×1
r ×1
sse ×1
x86 ×1
xunit ×1