Lau*_*ent 2 setvalue dataframe pandas python-polars
在 Pandas 中,您可以使用at属性更新值,如下所示:
import pandas as pd
df = pd.DataFrame({"col1": [1, 2, 3], "col2": [4, 5, 6]})
df.at[2, "col2"] = 99
print(df)
# Output
col1 col2
0 1 4
1 2 5
2 3 99
Run Code Online (Sandbox Code Playgroud)
在 Polars 中执行此操作的惯用方法是什么?
您可以使用极地方括号索引:
\ndf = pl.DataFrame({"col1": [1, 2, 3], "col2": [4, 5, 6]})\n\xe2\x80\x8b\ndf[2, "col2"] = 99 #same syntax as pandas but without the `.at`\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x80\x8b\n输出:
\nprint(df)\n\xe2\x80\x8b\nshape: (3, 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\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\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 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\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 4 \xe2\x94\x82\n\xe2\x94\x82 2 \xe2\x94\x86 5 \xe2\x94\x82\n\xe2\x94\x82 3 \xe2\x94\x86 99 \xe2\x94\x82\n\xe2\x94\x94\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\x80\xe2\x94\x98\nRun Code Online (Sandbox Code Playgroud)\n