如何比较两个极坐标DataFrames的值是否相等?看起来==只有当两个表是同一个对象时才是正确的:
import polars as pl
pl.DataFrame({"x": [1,2,3]}) == pl.DataFrame({"x": [1,2,3]}) # False
Run Code Online (Sandbox Code Playgroud) 我需要在数据框中创建一个新列来存储处理后的值。所以我使用了 Polars apply 函数对 dicom 进行一些处理,然后返回值。但此应用函数默认将整列视为极坐标系列,并且不会逐行处理。
df = df.with_columns(
[
pl.apply(
exprs=["Filename", "Dicom_Tag", "Dicom_Tag_Corrected", "Name"],
f=apply_corrections_polars,
).alias("dicom_tag_value_corrected"),
]
)
Run Code Online (Sandbox Code Playgroud) 在 pandas 中,它会自动发生,只需通过调用pd.concat([df1, df2, df3]),之前没有该列的框架就会获得一个填充有NaNs 的列。
在极坐标中,我收到一条'shape error'消息,表明列不同(11 列df1vs 12 列df2)。
我想知道如何将 Spark 数据帧转换为 Polars 数据帧。
假设我在 PySpark 上有这段代码:
df = spark.sql('''select * from tmp''')
Run Code Online (Sandbox Code Playgroud)
我可以使用 轻松将其转换为 pandas 数据框.toPandas。极坐标中有类似的东西吗,因为我需要获取极坐标数据帧以进行进一步处理?
目前,当我尝试从极坐标日期时间列中检索日期时,我必须写一些东西。如同:
df = pl.DataFrame({
'time': [dt.datetime.now()]
})
df = df.select([
pl.col("*"),
pl.col("time").apply(lambda x: x.date()).alias("date")
])
Run Code Online (Sandbox Code Playgroud)
有没有不同的方式,更接近:
pl.col("time").dt.date().alias("date")
Run Code Online (Sandbox Code Playgroud) 我用极地代替熊猫。我对速度和惰性计算/评估感到非常惊讶。目前,有很多关于惰性数据帧的方法,但它们只能驱使我到目前为止。
因此,我想知道将极坐标与其他工具结合使用以实现更复杂的操作(例如回归/模型拟合)的最佳方法是什么。
更具体地说,我将举一个涉及线性回归的例子。
假设我有一个包含 day、y、x1 和 x2 列的极坐标数据框,并且我想生成一个序列,它是按天对 x1 和 x2 进行回归 y 的残差。我包含了如下代码示例以及如何使用 pandas 和 statsmodels 解决它。如何使用惯用的极坐标以最有效的方式获得相同的结果?
import pandas as pd
import statsmodels.api as sm
def regress_resid(df, yvar, xvars):
result = sm.OLS(df[yvar], sm.add_constant(df[xvars])).fit()
return result.resid
df = pd.DataFrame(
{
"day": [1, 1, 1, 1, 1, 2, 2, 2, 2, 2],
"y": [1, 6, 3, 2, 8, 4, 5, 2, 7, 3],
"x1": [1, 8, 2, 3, 5, 2, 1, 2, 7, 3],
"x2": [8, 5, 3, 6, 3, 7, …Run Code Online (Sandbox Code Playgroud) 在 pandas 中,以下代码会将 col1 中的字符串拆分为许多列。有没有办法在极地做到这一点?
d = {'col1': ["a/b/c/d", "a/b/c/d"]}
df= pd.DataFrame(data=d)
df[["a","b","c","d"]]=df["col1"].str.split('/',expand=True)
Run Code Online (Sandbox Code Playgroud) 在 Pandas 中,我们可以使用该map函数将字典映射到系列,以使用映射值创建另一个系列。更一般地说,我相信它调用参数的索引运算符,即[]。
import pandas as pd
dic = { 1: 'a', 2: 'b', 3: 'c' }
pd.Series([1, 2, 3, 4]).map(dic) # returns ["a", "b", "c", NaN]
Run Code Online (Sandbox Code Playgroud)
我还没有找到直接在 Polars 中执行此操作的方法,但找到了一些替代方案。这些是否是推荐的方法,或者是否有更好的方法?
import polars as pl
dic = { 1: 'a', 2: 'b', 3: 'c' }
# Approach 1 - apply
pl.Series([1, 2, 3, 4]).apply(lambda v: dic.get(v, None)) # returns ["a", "b", "c", null]
# Approach 2 - left join
(
pl.Series([1, 2, 3, 4])
.alias('key')
.to_frame()
.join( …Run Code Online (Sandbox Code Playgroud) 我有一个以下形式的 Polars 数据框:
\ndf = pl.DataFrame({\'a\':[1,2,3], \'b\':[[\'a\',\'b\'],[\'a\'],[\'c\',\'d\']]}) \nRun Code Online (Sandbox Code Playgroud)\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\x80\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\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 i64 \xe2\x94\x86 list[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\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\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 1 \xe2\x94\x86 ["a", "b"] \xe2\x94\x82\n\xe2\x94\x82 2 \xe2\x94\x86 ["a"] \xe2\x94\x82\n\xe2\x94\x82 3 \xe2\x94\x86 ["c", "d"] \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\x80\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我想将其转换为以下形式。我计划保存到 parquet 文件,并查询该文件(使用 sql)。
\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\x90\n\xe2\x94\x82 a \xe2\x94\x86 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\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 "a" \xe2\x94\x82\n\xe2\x94\x82 1 \xe2\x94\x86 "b" \xe2\x94\x82\n\xe2\x94\x82 2 \xe2\x94\x86 "a" \xe2\x94\x82\n\xe2\x94\x82 3 \xe2\x94\x86 "c" \xe2\x94\x82\n\xe2\x94\x82 3 \xe2\x94\x86 "d" \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\x98\nRun Code Online (Sandbox Code Playgroud)\n我看到了一个适用于 struct …
我正在使用下面的数据框转换为特定格式的字典。
但是,我收到错误 TypeError: unhashable type: 'Series'
import polars as pl
#input (polars eager dataframe):
polar_df = pl.DataFrame(
"foo": ['a', 'b', 'c'],
"bar": [[6.0, 7.0, 8.0],[9.0,10.0,11.0],[12.0,13.0,14.0]]
)
#expected output (dictionary):
#{'a':[6.0, 7.0, 8.0],'b':[9.0,10.0,11.0],'c':[12.0,13.0,14.0]}
dict_output =
dict(zip(polar_df.select(pl.col('foo')),
polar_df.select(pl.col('bar'))
))
Run Code Online (Sandbox Code Playgroud)