如果我有一个 Polars 文字,如何提取该值?
import polars as pl
expr = pl.lit(0.5)
val = float(expr)
# TypeError: float() argument must be a string or a real number, not 'Expr'
Run Code Online (Sandbox Code Playgroud) 我试图搜索是否有一种方法可以轻松更改带有数字的字符串的数据类型。比如我面临的问题如下:
df = pl.Dataframe({"foo":
["100CT pen", "pencils 250CT", "what 125CT soever", "this is a thing"]}
)
Run Code Online (Sandbox Code Playgroud)
我可以提取并创建一个名为 的新列{"bar": ["100", "250", "125", ""]}。但后来我找不到一个方便的函数来将此列转换为 Int64 或 float dtypes,以便结果为[100, 250, 125, null].
另外,反之亦然。[100, 250, 125, 0]有时,有一个方便的函数将的 列转换为 会很有用["100", "250", "125", "0"]。它是已经存在的东西吗?
我正在尝试使用以下代码循环遍历 Polars 记录集:
\n\nimport polars as pl\n\nmydf = pl.DataFrame(\n {"start_date": ["2020-01-02", "2020-01-03", "2020-01-04"],\n "Name": ["John", "Joe", "James"]})\n\nprint(mydf)\n\n\xe2\x94\x82start_date \xe2\x94\x86 Name \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 str \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\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\xa1\n\xe2\x94\x82 2020-01-02 \xe2\x94\x86 John \xe2\x94\x82\n\xe2\x94\x82 2020-01-03 \xe2\x94\x86 Joe \xe2\x94\x82\n\xe2\x94\x82 2020-01-04 \xe2\x94\x86 James \xe2\x94\x82\n\nfor row in mydf.rows():\n print(row)\n\n('2020-01-02', 'John')\n('2020-01-03', 'Joe')\n('2020-01-04', 'James')\n\nRun Code Online (Sandbox Code Playgroud)\n有没有办法使用命名列而不是索引来专门引用“名称”?在 Pandas 中,这看起来像:
\nimport pandas as pd\n\nmydf = pd.DataFrame(\n {"start_date": ["2020-01-02", "2020-01-03", "2020-01-04"],\n "Name": ["John", "Joe", "James"]})\n\nfor index, row in mydf.iterrows():\n mydf['Name'][index]\n\n'John'\n'Joe'\n'James'\nRun Code Online (Sandbox Code Playgroud)\n 在Polars中,如何为 中的所有列指定单一 dtype read_csv?
根据docs, 的dtypes参数read_csv可以采用 形式的映射(字典){'column_name': dtype},也可以采用 dtypes 列表,每一列一个。但是,尚不清楚如何指定“我希望所有列都是单一数据类型”。
例如,如果您希望所有列均为 Utf-8 并且您知道列总数,则可以执行以下操作:
pl.read_csv('sample.csv', dtypes=[pl.Utf8]*number_of_columns)
Run Code Online (Sandbox Code Playgroud)
但是,如果您不知道总列数,则此方法不起作用。在 Pandas 中,你可以这样做:
pd.read_csv('sample.csv', dtype=str)
Run Code Online (Sandbox Code Playgroud)
但这在Polars中不起作用。
我发现可以使用系列名称空间进行附加(/sf/answers/4941934161/)。我想知道是否有类似的方法来附加或连接数据帧。
从pandas历史上看,这可以通过df1.append(df2). 但是,该方法已被弃用(如果尚未被弃用)pd.concat([df1, df2])。
df1
| A | 乙 | C |
|---|---|---|
| 1 | 2 | 3 |
df2
| A | 乙 | C |
|---|---|---|
| 4 | 5 | 6 |
资源
| A | 乙 | C |
|---|---|---|
| 1 | 2 | 3 |
| 4 | 5 | 6 |
我想用 Polars 替换 Pandas,但我无法找到如何在不转换为 Pandas 的情况下将 Polars 与 Plotly 一起使用。我想知道是否有一种方法可以将 Pandas 完全排除在这个过程之外。
考虑以下测试数据:
import polars as pl
import numpy as np
import plotly.express as px
df = pl.DataFrame(
{
"nrs": [1, 2, 3, None, 5],
"names": ["foo", "ham", "spam", "egg", None],
"random": np.random.rand(5),
"groups": ["A", "A", "B", "C", "B"],
}
)
fig = px.bar(df, x='names', y='random')
fig.show()
Run Code Online (Sandbox Code Playgroud)
我希望这段代码在 Jupyter 笔记本中显示条形图,但它返回一个错误:
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/polars/internals/frame.py:1483: UserWarning: accessing series as Attribute of a DataFrame is deprecated
warnings.warn("accessing series as Attribute of a DataFrame is deprecated") …Run Code Online (Sandbox Code Playgroud) 有没有一种优雅的方法来重新编码极坐标数据框中的值。
例如
1->0,
2->0,
3->1...
Run Code Online (Sandbox Code Playgroud)
在 Pandas 中,它很简单:
df.replace([1,2,3,4,97,98,99],[0,0,1,1,2,2,2])
Run Code Online (Sandbox Code Playgroud) 如何使用索引添加新功能,例如数据帧长度和删除行值。我想添加一个新列,我可以在其中计算数据框中可用的行数,并使用索引删除行值。
for i in range(len(df)):
if (df['col1'][i] == df['col2'][i]) and (df['col4'][i] == df['col3'][i]):
pass
elif (df['col1'][i] == df['col3'][i]) and (df['col4'][i] == df['col2'][i]):
df['col1'][i] = df['col2'][i]
df['col4'][i] = df['col3'][i]
else:
df = df.drop(i)
Run Code Online (Sandbox Code Playgroud) 在pandas\xef\xbc\x9a
df[\'new\'] = a\nRun Code Online (Sandbox Code Playgroud)\n其中a是一个数字系列或只是一个数字。
\n同时polars我们可以添加一个char
df.with_column(\n [\n pl.all(),\n pl.lit(\'str\').alias(\'new\')\n ]\n)\nRun Code Online (Sandbox Code Playgroud)\n但如何添加数字系列或数字作为新列polars?
\n请注意,新的数值系列不在原始中df,它是一些计算的结果。
我正在寻找类似的功能
df.groupby('column').agg(sample(10))
Run Code Online (Sandbox Code Playgroud)
这样我就可以从每组中随机选择十个左右的元素。
这是专门为了让我可以读取 LazyFrame 并使用每个组的小样本而不是整个数据帧。
一种近似解是:
df = lf.groupby('column').agg(
pl.all().sample(.001)
)
df = df.explode(df.columns[1:])
Run Code Online (Sandbox Code Playgroud)
该近似解决方案与对整个数据帧进行采样并随后进行分组相同。不好。