这主要是一个关于 python 的问题:How x in [a, b, c]isvalued different from x in {a, b, c}.
我正在努力解决的上下文是这样的:
import polars as pl
s = pl.Series(["a", "b"], dtype=pl.Categorical)
s.dtype in [pl.Categorical, pl.Enum] # True
s.dtype in {pl.Categorical, pl.Enum} # False
s.dtype in {pl.Categorical: 1, pl.Enum: 2} # False
Run Code Online (Sandbox Code Playgroud)
我想更好地理解Python。我还想知道极地是否可以做任何事情来使第二种情况起作用,因为它目前看起来像一把步枪。
显示一个玩具示例,K=2但问题主要与高g基数和相关K>>1:
df = pl.DataFrame(dict(\n g=[1, 2, 1, 2, 1, 2],\n v=[1, 2, 3, 4, 5, 6],\n))\n\nK = 2\n\ndf.with_columns((col.v.shift(k+1).over(\'g\').alias(f\'s{k}\') for k in range(K)))\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x95\xad\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x95\xae\n\xe2\x94\x82 g \xe2\x94\x86 v \xe2\x94\x86 s0 \xe2\x94\x86 s1 \xe2\x94\x82\n\xe2\x94\x82 i64 \xe2\x94\x86 i64 \xe2\x94\x86 i64 \xe2\x94\x86 i64 \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 1 \xe2\x94\x86 1 \xe2\x94\x86 null \xe2\x94\x86 null \xe2\x94\x82\n\xe2\x94\x82 2 \xe2\x94\x86 2 \xe2\x94\x86 null \xe2\x94\x86 null \xe2\x94\x82\n\xe2\x94\x82 1 \xe2\x94\x86 3 \xe2\x94\x86 1 \xe2\x94\x86 null \xe2\x94\x82\n\xe2\x94\x82 2 \xe2\x94\x86 4 \xe2\x94\x86 2 \xe2\x94\x86 null \xe2\x94\x82\n\xe2\x94\x82 1 …Run Code Online (Sandbox Code Playgroud) 我想根据列的绝对值以升序或降序对极坐标数据框进行排序。Pandas在Python中或者使用sortedPython中的函数很容易做到。假设我想根据val下面的数据框中的列进行排序。
import numpy as np\nnp.random.seed(42)\nimport polars as pl\n\ndf = pl.DataFrame({\n "name": ["one", "one", "one", "two", "two", "two"],\n "id": ["C", "A", "B", "B", "C", "C"],\n "val": np.random.randint(-10, 10, 6)\n })\nRun Code Online (Sandbox Code Playgroud)\n返回:
\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 name \xe2\x94\x86 id \xe2\x94\x86 val \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 str \xe2\x94\x86 str \xe2\x94\x86 i32 \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 one \xe2\x94\x86 C \xe2\x94\x86 -4 \xe2\x94\x82\n\xe2\x94\x82 one \xe2\x94\x86 A \xe2\x94\x86 9 \xe2\x94\x82\n\xe2\x94\x82 one \xe2\x94\x86 B \xe2\x94\x86 4 \xe2\x94\x82\n\xe2\x94\x82 two \xe2\x94\x86 B \xe2\x94\x86 …Run Code Online (Sandbox Code Playgroud) import polars as pl
df = pl.DataFrame(
{
"X": [4, 2, 3, 4],
"Y": ["p", "p", "p", "p"],
"Z": ["b", "b", "b", "b"],
}
)
Run Code Online (Sandbox Code Playgroud)
我们知道pandas的等价物df.drop_duplicates()在python-polarsdf.unique()中
但是,每次执行查询时都会得到不同的结果?
print(df.unique())
Run Code Online (Sandbox Code Playgroud)
X Y Z
i64 str str
3 "p" "b"
2 "p" "b"
4 "p" "b"
Run Code Online (Sandbox Code Playgroud)
X Y Z
i64 str str
4 "p" "b"
2 "p" "b"
3 "p" "b"
Run Code Online (Sandbox Code Playgroud)
X Y Z
i64 str str
2 "p" "b"
3 "p" "b"
4 "p" "b"
Run Code Online (Sandbox Code Playgroud)
这是故意的吗?背后的原因是什么?
我发现列表作为一种类型非常有用。不幸的是,它们有详细记录 - 如何生成它们,但没有记录如何访问内容本身:(
例如,我有一个名为 CARS 的列:
CARS
list
[f64]
======
[1,2,3]
[2,3]
[4]
[]
Run Code Online (Sandbox Code Playgroud)
我想准备两个新变量。
结果应该是这样的:
CARF CAR2
1 2
2 1
4 3
Null 3
Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何将行聚合成批次(即滑动/翻滚窗口),这样如果我们有一个像这样的表
id x y
---- ---- ----
1 x1 y1
2 x2 y2
3 x3 y3
4 x4 y4
5 x5 y5
Run Code Online (Sandbox Code Playgroud)
结果将是
id x y Grouped x3
---- ---- ---- -----------------------------------------
1 x1 y1 None
2 x2 y2 None
3 x3 y3 [[1, x1, y1], [2, x2, y2], [3, x3, y3]]
4 x4 y4 [[2, x2, y2], [3, x3, y3], [4, x4, y4]]
5 x5 y5 [[3, x3, y3], [4, x4, y4], [5, x5, y5]]
Run Code Online (Sandbox Code Playgroud)
我目前有一个聚合列,使用concat_list以下方法:
id …Run Code Online (Sandbox Code Playgroud) 假设我有一个极坐标数据框:
import polars as pl
df = pl.DataFrame({'index': [1,2,3,2,1],
'object': [1, 1, 1, 2, 2],
'period': [1, 2, 4, 4, 23],
'value': [24, 67, 89, 5, 23],
})
Run Code Online (Sandbox Code Playgroud)
我如何获得索引的字典 -> 最后一个值
df.col('value').last().over(['index']).alias("last")是最后一个值,但这需要大量额外的计算和更多的工作才能获取键值对。
我想通过根据数量列中的值重复行来扩展以下Polars数据框。
原始数据框:
| 水果 | 数量 |
|---|---|
| 苹果 | 2 |
| 香蕉 | 3 |
预期输出:
| 水果 | 数量 |
|---|---|
| 苹果 | 1 |
| 苹果 | 1 |
| 香蕉 | 1 |
| 香蕉 | 1 |
| 香蕉 | 1 |
这是一个非常相似的问题,但使用 Pandas 而不是 Polars:Repeat rows in a pandas DataFrame based on column value
Polars 重复函数似乎没有提供与 Pandas 对应函数相同的功能:https://pola-rs.github.io/polars/py-polars/html/reference/api/polars.repeat.html
我是 Polars 新用户。熊猫有df.dropna。我需要替换此功能,但我还没有在极地找到 dropna。drona在 Polars 用户指南中搜索当前没有结果。
我的具体问题:将以下语句从 pandas 转换为 Polars
\ndf.dropna(subset=list_of_vars, thresh=1)
我想我应该使用df.filter. 我不知道先验的内容list_of_vars,因此制作一组|过滤器有点棘手。所有变量list_of_vars都是数据框中的列
输入
\nimport polars as pl\n\ndf = pl.DataFrame(\n {\n \'col1\':[0,float(\'nan\'),2,3],\n \'col2\':[0,float(\'nan\'),float(\'nan\'),3],\n \'col3\':[0,1,2,3],\n \'col3\':[float(\'nan\'),float(\'nan\'),float(\'nan\'),float(\'nan\')]\n }\n)\ndf\nlist_of_vars = [\'col1\', \'col2\']\nRun Code Online (Sandbox Code Playgroud)\n期望的输出:
\n仅保留数据框中至少一列的值list_of_vars不为 NaN/null 的行。
\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 col1 \xe2\x94\x86 col2 \xe2\x94\x86 col3 \xe2\x94\x86 col4 \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 f64 \xe2\x94\x86 f64 \xe2\x94\x86 i64 \xe2\x94\x86 f64 …Run Code Online (Sandbox Code Playgroud) 我有一个数据框如下。
pl.DataFrame({'combine_address':[ ["Yes|#456 Lane|Apt#4|ABC|VA|50566", "Yes|#456 Lane|Apt#4|ABC|VA|50566", "No|#456 Lane|Apt#4|ABC|VA|50566"],
["No|#8495|APT#94|SWE|WA|43593", "No|#8495|APT#94|SWE|WA|43593", "Yes|#8495|APT#94|SWE|WA|43593"]
]})
Run Code Online (Sandbox Code Playgroud)
这里组合地址是一个列表类型列,其中包含大约 6 个管道(|)值的元素,这里我想使用列表中的分隔符(|)对每个元素进行分割。
这是预期的输出:
如果列表有 3 个元素,则拆分列将为 3*6=18
如果列表有 5 个元素,则拆分列将为 5*6=30,依此类推。