我有以下数据框:
\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) 我的工作有 df 3 个主要列:cid1, cid2, cid3,以及更多 7 个列cid4, cid5, etc。
cid1是cid2,int另一列是float。
cid1和的每个组合cid2都是一个包含某些行的工作集,其中所有其他列的值都不同。我想过滤 df 并接收我的 df ,其中仅包含和cid3的每个组合的列中的最大值。下一列必须保持不变。cid1cid2cid4
这段代码帮助我完成了部分任务:
df = (df
.groupby(["cid1", "cid2"])
.agg([pl.max("cid3").alias("max_cid3")])
)
Run Code Online (Sandbox Code Playgroud)
它只接收 3 列:cid1、cid2,并在不是最大值max_cid3时过滤所有行。cid3但我无法找到如何cid4, etc在不进行更改的情况下接收该行的所有其他列 ( )。
df = (df
.groupby(["cid1", "cid2"])
.agg([pl.max("cid3").alias("max_cid3"), pl.col("cid4")])
)
Run Code Online (Sandbox Code Playgroud)
我尝试添加pl.col("cid4")到 aggs 列表,但在列中我看到某些值的不同列表cid4。
我怎样才能正确地做到这一点?也许 Polars 有另一种方法可以实现 groupby 功能?
在 …
我正在尝试删除所选列列表中的空值。但看来我的操作可能with_columns不太对劲。如果您只想对选定的列进行删除操作,正确的方法是什么?
df = pl.DataFrame(\n {\n "id": ["NY", "TK", "FD"], \n "eat2000": [1, None, 3], \n "eat2001": [-2, None, 4],\n "eat2002": [None, None, None],\n "eat2003": [-9, None, 8],\n "eat2004": [None, None, 8]\n }\n); df\n\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\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\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\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\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\x90\n\xe2\x94\x82 id \xe2\x94\x86 eat2000 \xe2\x94\x86 eat2001 \xe2\x94\x86 eat2002 \xe2\x94\x86 eat2003 \xe2\x94\x86 eat2004 \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 str \xe2\x94\x86 i64 \xe2\x94\x86 i64 \xe2\x94\x86 f64 \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\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\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\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\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\xa1\n\xe2\x94\x82 NY \xe2\x94\x86 1 \xe2\x94\x86 -2 \xe2\x94\x86 null \xe2\x94\x86 …Run Code Online (Sandbox Code Playgroud)