- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
macos: monterey
node: v18.1.0
nodejs-polars: 0.5.3
用该列的平均值减去 polars DataFrame 中的每一列。
由于 DataFrame.sub(other, axis='columns', level=None, fill_value=None)
,在 pandas 中解决方案非常简洁。 other
是标量、序列、系列或 DataFrame
:
df.sub(df.mean())
df - df.mean()
虽然在 nodejs-polars function 中,other
根据 sub: (other) => wrap("sub", prepareOtherArg 似乎只是一个
.Series
(其他).inner())
console.log(df)
┌─────────┬─────────┬─────────┬─────────┐
│ A ┆ B ┆ C ┆ D │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═════════╪═════════╪═════════╪═════════╡
│ 13520 ┆ -16 ┆ 384 ┆ 208 │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ 13472 ┆ -16 ┆ 384 ┆ 176 │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ 13456 ┆ -16 ┆ 368 ┆ 160 │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ 13472 ┆ -16 ┆ 368 ┆ 160 │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ 13472 ┆ -16 ┆ 352 ┆ 176 │
└─────────┴─────────┴─────────┴─────────┘
console.log(df.mean())
┌─────────┬─────────┬─────────┬─────────┐
│ A ┆ B ┆ C ┆ D │
│ --- ┆ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ f64 ┆ f64 │
╞═════════╪═════════╪═════════╪═════════╡
│ 13478.4 ┆ -16.0 ┆ 371.2 ┆ 176.0 │
└─────────┴─────────┴─────────┴─────────┘
df.sub(df.mean())
Error: Failed to determine supertype of Int64 and Struct([Field { name: "A", dtype: Int32 }, Field { name: "B", dtype: Int32 }, Field { name: "C", dtype: Int32 }, Field { name: "D", dtype: Int32 }])
df.sub(pl.Series(df.mean().row(0)))
Program crashes due to memory problems.
经过一些调查,我注意到了 tests :
test("sub", () => {
const actual = pl.DataFrame({
"foo": [1, 2, 3],
"bar": [4, 5, 6]
}).sub(1);
const expected = pl.DataFrame({
"foo": [0, 1, 2],
"bar": [3, 4, 5]
});
expect(actual).toFrameEqual(expected);
});
test("sub:series", () => {
const actual = pl.DataFrame({
"foo": [1, 2, 3],
"bar": [4, 5, 6]
}).sub(pl.Series([1, 2, 3]));
const expected = pl.DataFrame({
"foo": [0, 0, 0],
"bar": [3, 3, 3]
});
expect(actual).toFrameEqual(expected);
});
nodejs-polars 现在似乎无法优雅地完成这个任务。所以我目前的解决方案有点麻烦:逐列执行操作然后连接结果。
pl.concat(df.columns.map((col) => df.select(col).sub(df.select(col).mean(0).toSeries())), {how:'horizontal'})
有更好或更简单的方法吗?
我刚刚提出了一个更简单的解决方案,但它很难理解,我仍在努力弄清楚幕后发生了什么。
df.select(pl.col('*').sub(pl.col('*').mean()))
最佳答案
你用 [python-polars] 标记了这个问题,所以我将提供一个使用 Polars 和 Python 的解决方案。 (也许您可以将其转换为 Node-JS。)
从我们的数据开始:
import polars as pl
df = pl.DataFrame(
{
"A": [13520, 13472, 13456, 13472, 13472],
"B": [-16, -16, -16, -16, -16],
"C": [384, 384, 368, 368, 352],
"D": [208, 176, 160, 160, 176],
}
)
df
shape: (5, 4)
┌───────┬─────┬─────┬─────┐
│ A ┆ B ┆ C ┆ D │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═══════╪═════╪═════╪═════╡
│ 13520 ┆ -16 ┆ 384 ┆ 208 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 13472 ┆ -16 ┆ 384 ┆ 176 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 13456 ┆ -16 ┆ 368 ┆ 160 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 13472 ┆ -16 ┆ 368 ┆ 160 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 13472 ┆ -16 ┆ 352 ┆ 176 │
└───────┴─────┴─────┴─────┘
我们可以非常简洁地解决这个问题:
df.with_columns([
(pl.all() - pl.all().mean()).suffix('_centered')
])
shape: (5, 8)
┌───────┬─────┬─────┬─────┬────────────┬────────────┬────────────┬────────────┐
│ A ┆ B ┆ C ┆ D ┆ A_centered ┆ B_centered ┆ C_centered ┆ D_centered │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
╞═══════╪═════╪═════╪═════╪════════════╪════════════╪════════════╪════════════╡
│ 13520 ┆ -16 ┆ 384 ┆ 208 ┆ 41.6 ┆ 0.0 ┆ 12.8 ┆ 32.0 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 13472 ┆ -16 ┆ 384 ┆ 176 ┆ -6.4 ┆ 0.0 ┆ 12.8 ┆ 0.0 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 13456 ┆ -16 ┆ 368 ┆ 160 ┆ -22.4 ┆ 0.0 ┆ -3.2 ┆ -16.0 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 13472 ┆ -16 ┆ 368 ┆ 160 ┆ -6.4 ┆ 0.0 ┆ -3.2 ┆ -16.0 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 13472 ┆ -16 ┆ 352 ┆ 176 ┆ -6.4 ┆ 0.0 ┆ -19.2 ┆ 0.0 │
└───────┴─────┴─────┴─────┴────────────┴────────────┴────────────┴────────────┘
如果要覆盖列,可以去掉suffix
表达式:
df.with_columns([
(pl.all() - pl.all().mean())
])
shape: (5, 4)
┌───────┬─────┬───────┬───────┐
│ A ┆ B ┆ C ┆ D │
│ --- ┆ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ f64 ┆ f64 │
╞═══════╪═════╪═══════╪═══════╡
│ 41.6 ┆ 0.0 ┆ 12.8 ┆ 32.0 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ -6.4 ┆ 0.0 ┆ 12.8 ┆ 0.0 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ -22.4 ┆ 0.0 ┆ -3.2 ┆ -16.0 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ -6.4 ┆ 0.0 ┆ -3.2 ┆ -16.0 │
├╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ -6.4 ┆ 0.0 ┆ -19.2 ┆ 0.0 │
└───────┴─────┴───────┴───────┘
编辑:本质上,polars.all
或 polars.col('*')
为每一列复制了一个完整的表达式,这样:
pl.col('*') - pl.col('*').mean()
是语法糖:
[
pl.col('A') - pl.col('A').mean(),
pl.col('B') - pl.col('B').mean(),
pl.col('C') - pl.col('C').mean(),
pl.col('D') - pl.col('D').mean(),
]
关于pandas - 如何在 polars DataFrame 中的每一列与该列的平均值之间轻松执行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72539701/
假设我有 3 个 DataFrame。其中一个 DataFrame 的列名不在其他两个中。 using DataFrames df1 = DataFrame([['a', 'b', 'c'], [1,
假设我有 3 个 DataFrame。其中一个 DataFrame 的列名不在其他两个中。 using DataFrames df1 = DataFrame([['a', 'b', 'c'], [1,
我有一个 largeDataFrame(多列和数十亿行)和一个 smallDataFrame(单列和 10,000 行)。 只要 largeDataFrame 中的 some_identifier 列
我有一个函数,可以在其中规范化 DataFrame 的前 N 列。我想返回规范化的 DataFrame,但不要管原来的。然而,该函数似乎也会对传递的 DataFrame 进行变异! using D
我想在 Scala 中使用指定架构在 DataFrame 上创建。我尝试过使用 JSON 读取(我的意思是读取空文件),但我认为这不是最佳实践。 最佳答案 假设您想要一个具有以下架构的数据框: roo
我正在尝试从数据框中删除一些列,并且不希望返回修改后的数据框并将其重新分配给旧数据框。相反,我希望该函数只修改数据框。这是我尝试过的,但它似乎并没有做我所除外的事情。我的印象是参数是作为引用传递的,而
我有一个包含大约 60000 个数据的庞大数据集。我会首先使用一些标准对整个数据集进行分组,接下来我要做的是将整个数据集分成标准内的许多小数据集,并自动对每个小数据集运行一个函数以获取参数对于每个小数
我遇到了以下问题,并有一个想法来解决它,但没有成功:我有一个月内每个交易日的 DAX 看涨期权和看跌期权数据。经过转换和一些计算后,我有以下 DataFrame: DaxOpt 。现在的目标是消除没有
我正在尝试做一些我认为应该是单行的事情,但我正在努力把它做好。 我有一个大数据框,我们称之为lg,还有一个小数据框,我们称之为sm。每个数据帧都有一个 start 和一个 end 列,以及多个其他列所
我有一个像这样的系列数据帧的数据帧: state1 state2 state3 ... sym1 sym
我有一个大约有 9k 行和 57 列的数据框,这是“df”。 我需要一个新的数据框:'df_final'- 对于“df”的每一行,我必须将每一行复制“x”次,并将每一行中的日期逐一增加,也就是“x”次
假设有一个 csv 文件如下: # data.csv 0,1,2,3,4 a,3.0,3.0,3.0,3.0,3.0 b,3.0,3.0,3.0,3.0,3.0 c,3.0,3.0,3.0,3.0,3
我只想知道是否有人对以下问题有更优雅的解决方案: 我有两个 Pandas DataFrame: import pandas as pd df1 = pd.DataFrame([[1, 2, 3], [
我有一个 pyspark 数据框,我需要将其转换为 python 字典。 下面的代码是可重现的: from pyspark.sql import Row rdd = sc.parallelize([R
我有一个 DataFrame,我想在 @chain 的帮助下对其进行处理。如何存储中间结果? using DataFrames, Chain df = DataFrame(a = [1,1,2,2,2
我有一个包含 3 列的 DataFrame,名为 :x :y 和 :z,它们是 Float64 类型。 :x 和 "y 在 (0,1) 上是 iid uniform 并且 z 是 x 和 y 的总和。
这个问题在这里已经有了答案: pyspark dataframe filter or include based on list (3 个答案) 关闭 2 年前。 只是想知道是否有任何有效的方法来过
我刚找到这个包FreqTables ,它允许人们轻松地从 DataFrames 构建频率表(我正在使用 DataFrames.jl)。 以下代码行返回一个频率表: df = CSV.read("exa
是否有一种快速的方法可以为 sort 指定自定义订单?/sort!在 Julia DataFrames 上? julia> using DataFrames julia> srand(1); juli
在 Python Pandas 和 R 中,可以轻松去除重复的列 - 只需加载数据、分配列名,然后选择那些不重复的列。 使用 Julia Dataframes 处理此类数据的最佳实践是什么?此处不允许
我是一名优秀的程序员,十分优秀!