如何比较两个极坐标DataFrames的值是否相等?看起来==只有当两个表是同一个对象时才是正确的:
import polars as pl
pl.DataFrame({"x": [1,2,3]}) == pl.DataFrame({"x": [1,2,3]}) # False
Run Code Online (Sandbox Code Playgroud) 在极坐标中,我可以获得水平最大值(到达行的一组列的最大值),如下所示:
\ndf = pl.DataFrame(\n {\n "a": [1, 8, 3],\n "b": [4, 5, None],\n }\n)\n\ndf.with_columns(max = pl.max_horizontal("a", "b"))\n\xe2\x94\x8c\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\x90\n\xe2\x94\x82 a \xe2\x94\x86 b \xe2\x94\x86 max \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 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\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 1 \xe2\x94\x86 4 \xe2\x94\x86 4 \xe2\x94\x82\n\xe2\x94\x82 8 \xe2\x94\x86 5 \xe2\x94\x86 8 \xe2\x94\x82\n\xe2\x94\x82 3 \xe2\x94\x86 null \xe2\x94\x86 3 \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\nRun Code Online (Sandbox Code Playgroud)\n这对应于 Pandas df[["a", "b"]].max(axis=1)。
现在,我如何获取列名称而不是实际的最大值?\n换句话说,Pandas' 的 Polars 版本是什么df[CHANGE_COLS].idxmax(axis=1)?
预期输出为:
\n\xe2\x94\x8c\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\x90\n\xe2\x94\x82 a \xe2\x94\x86 b \xe2\x94\x86 max \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- …Run Code Online (Sandbox Code Playgroud) 我目前正在学习 R。特别是我需要记住 -function 系列中的函数apply(例如lapply、sapply、mapply等)。我知道apply-function 系列中的每个函数的作用(特别是这个答案很有帮助),但有时会混淆名称。
每个 apply 函数的前缀是否有自然含义(例如lapply = 'list'-apply)?
当我使用时,我没有得到与极地熊猫相同的输出to_dict。
示例 1。
df = pd.DataFrame({
'column_1': [1, 2, 1,4,5],
'column_2': ['Alice', 'Bob', 'Alice','Tom', 'Tom'],
'column_3': ['Alice1', 'Bob', 'Alice2','Tom', 'Tom']
})
test = df.to_dict(orient='records')
print('PANDAS',test)
Run Code Online (Sandbox Code Playgroud)
带输出
[{'column_1': 1, 'column_2': 'Alice', 'column_3': 'Alice1'}, {'column_1': 2, 'column_2': 'Bob', 'column_3': 'Bob'}, {'column_1': 1, 'column_2': 'Alice', 'column_3': 'Alice2'}, {'column_1': 4, 'column_2': 'Tom', 'column_3': 'Tom'}, {'column_1': 5, 'column_2': 'Tom', 'column_3': 'Tom'}]
Run Code Online (Sandbox Code Playgroud)
示例 2.
dfPolars = pl.DataFrame({
'column_1': [1, 2, 1,4,5],
'column_2': ['Alice', 'Bob', 'Alice','Tom', 'Tom'],
'column_3': ['Alice1', 'Bob', 'Alice2','Tom', 'Tom']
})
testpolars = …Run Code Online (Sandbox Code Playgroud) 我有一个 LazyFrame,其中包含几个时期内的多列每小时数据。对于每个周期,我想找到涉及多列数学运算的函数的 x 值,以最小化结果。
\n我使用 scipy.optimize.minimize 来完成此操作,并且实际上获得了所需的结果。问题是这个过程运行得非常慢,所以我只是在寻找任何能完成相同但更快的事情。
\n def minimization_target(x, period_start):\n return hourly_data.filter(pl.col('period_start') == period_start).select((((pl.col('price').median() * pl.col('quantity').median() - (pl.col('estimated_quantity') * (pl.col('estimated_price') + x)).sum()) / (pl.col('key_product') * (pl.col('estimated_price') + x)).sum())).abs() - 1).abs()).collect().item()\n\n results = hourly_data.group_by('period_start', maintain_order=True).map_groups(lambda group: pl.DataFrame({'x_values': scipy.optimize.minimize(minimization_target, group.get_column('initial_guess').median(), args=group.get_column('period_start').median()).x}), schema=None)\nRun Code Online (Sandbox Code Playgroud)\n最小的例子:
\nimport scipy\nimport polars as pl\nfrom datetime import datetime\n\nhourly_data = pl.DataFrame({'period': [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3], 'price': …Run Code Online (Sandbox Code Playgroud)