我在 Python 中有以下极坐标 DF
df = pl.DataFrame({
"user_movies": [[7064, 7153, 78009], [6, 7, 1042], [99, 110, 3927], [2, 11, 152081], [260, 318, 195627]],
"user_ratings": [[5.0, 5.0, 5.0], [4.0, 2.0, 4.0], [4.0, 4.0, 3.0], [3.5, 3.0, 4.0], [1.0, 4.5, 0.5]],
"common_movies": [[7064, 7153], [7], [110, 3927], [2], [260, 195627]]
})
print(df.head())
Run Code Online (Sandbox Code Playgroud)
我想创建一个名为“common_movie_ ratings”的新列,该列将从每个评级列表中仅获取常见电影中评级的电影的索引。例如,对于第一行,我应该仅返回电影的评分 [7064, 7153,],对于第二行,我应该返回电影的评分 [7],依此类推。
为此,我创建了以下函数:
def get_common_movie_ratings(row): #Each row is a tuple of arrays.
common_movies = row[2] #the index of the tuple denotes the 3rd array, which represents the …Run Code Online (Sandbox Code Playgroud) 我有以下数据框:
\ndf = pl.DataFrame({\n "Column A": [2, 3, 1, 4, 1, 3, 3, 2, 1, 0],\n "Column B": [\n "Life", None, None, None, "Death", None, \n "Life", None, None, "Death"\n ]\n})\nRun Code Online (Sandbox Code Playgroud)\nshape: (10, 2)\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\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\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 Column A \xe2\x94\x86 Column B \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 i64 \xe2\x94\x86 str \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\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\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 2 \xe2\x94\x86 Life \xe2\x94\x82\n\xe2\x94\x82 3 \xe2\x94\x86 null \xe2\x94\x82\n\xe2\x94\x82 1 \xe2\x94\x86 null \xe2\x94\x82\n\xe2\x94\x82 4 \xe2\x94\x86 null \xe2\x94\x82\n\xe2\x94\x82 1 \xe2\x94\x86 Death \xe2\x94\x82\n\xe2\x94\x82 3 \xe2\x94\x86 null \xe2\x94\x82\n\xe2\x94\x82 3 \xe2\x94\x86 Life \xe2\x94\x82\n\xe2\x94\x82 2 \xe2\x94\x86 …Run Code Online (Sandbox Code Playgroud) 我使用Polars库进行数据框操作。我有两个数据帧,我想使用根据条件从另一个数据帧获取的单个值来更新一个数据帧的列值。这是代码:
tmp = df[df['UnifiedInvoiceID'] == inv]
mask = (df_invoice_features['UnifiedInvoiceID'] == inv)
df_invoice_features[mask, 'UnifiedCustomerID'] = tmp[0, 'UnifiedCustomerID']
Run Code Online (Sandbox Code Playgroud)
并且,这是错误:
PySeries.new_u64() missing 1 required positional argument: '_strict'
Run Code Online (Sandbox Code Playgroud)
您认为为什么会返回这样的错误?
我有以下带有 pandas 的 Python 代码
df['EVENT_DATE'] = df.apply(
lambda row: datetime.date(year=row.iyear, month=row.imonth, day=row.iday).strftime("%Y-%m-%d"), axis=1)
Run Code Online (Sandbox Code Playgroud)
并希望将其转换为有效的 Polars 代码。有人有办法解决这个问题吗?
我刚刚开始在 python 中使用极坐标,我来自 pandas。我想知道如何在 python Polars 中复制下面的 pandas 代码
import pandas as pd
import polars as pl
df['exp_mov_avg_col'] = df.groupby('agg_col')['ewm_col'].transform(lambda x : x.ewm(span=14).mean())
Run Code Online (Sandbox Code Playgroud)
我已经尝试过以下方法:
df.groupby('agg_col').agg([pl.col('ewm_col').ewm_mean().alias('exp_mov_avg_col')])
Run Code Online (Sandbox Code Playgroud)
但这给了我每个提供者的指数移动平均值列表,我希望将该列表分配给原始数据帧中正确索引的列,就像 pandas 代码一样。
有没有办法允许 Polars 中的表达式引用先前的别名表达式?例如,定义两个新列的代码会出错,因为第二个新列引用第一个:
import polars as pl
df = pl.DataFrame(dict(x=[0, 0, 1]))
df.select([
(pl.col('x') + 1).alias('y'),
(pl.col('y') * 2).alias('z')],
)
# pyo3_runtime.PanicException: called `Result::unwrap()` on an `Err` value:
# NotFound("Unable to get field named \"y\". Valid fields: [\"x\"]")
Run Code Online (Sandbox Code Playgroud)
该错误表明失败是由于第一个别名对第二个表达式不可见而导致的。有没有一种简单的方法可以让这项工作发挥作用?
我有一个数据框,其中“PROGRAM”、“VERSION”和“RELEASE_DATE”列的每个组合有很多行。我想获得一个包含这三列的所有组合的数据框。groupby这会是或 的工作distinct吗?
谢谢
在极坐标数据框前面添加列的最惯用(且有效)的方法是什么?与此相同.with_column,但将其添加到索引 0 处?
我正在尝试将一些蟒蛇熊猫转换为极地。我一直试图将 pandas hub_table 函数转换为极坐标。以下是工作的 pandas 代码。我似乎无法通过 Polars 枢轴函数获得相同的行为。Polars 枢轴函数强制列参数并使用列值作为标题而不是列标签作为标题。我将使用下面相同的输出,但使用 Polars 而不是 Pandas。
df = pd.DataFrame({"obj" : ["ring", "shoe", "ring"], "price":["65", "42", "65"], "value":["53", "55", "54"], "date":["2022-02-07", "2022-01-07", "2022-03-07"]})
table = pd.pivot_table(df, values=['price','value','date'],index=['obj'], aggfunc={'price': pd.Series.nunique,'value':pd.Series.nunique,'date':pd.Series.nunique})
print(table)
Run Code Online (Sandbox Code Playgroud)
输出以下内容:
date price value
obj
ring 2 1 2
shoe 1 1 1
Run Code Online (Sandbox Code Playgroud) 我用.write_ipcPolars 存储为羽毛文件。原来数字串已经被保存为整数了。
因此,我需要在保存为feather之前或从feather读取之后将带有整数的列转换为字符串。我该如何使用 Polar 来做到这一点?