我有一个包含三列的数据集。将检查 A 列中的字符串。如果字符串匹配foo或spam,则同一行中其他两列L和 的值G应更改为XX。为此我尝试了以下方法。
df = pl.DataFrame(\n {\n "A": ["foo", "ham", "spam", "egg",],\n "L": ["A54", "A12", "B84", "C12"],\n "G": ["X34", "C84", "G96", "L6",],\n }\n)\nprint(df)\n\nshape: (4, 3)\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\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 L \xe2\x94\x86 G \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 str \xe2\x94\x86 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\xaa\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 foo \xe2\x94\x86 A54 \xe2\x94\x86 X34 \xe2\x94\x82\n\xe2\x94\x82 ham \xe2\x94\x86 A12 \xe2\x94\x86 C84 \xe2\x94\x82\n\xe2\x94\x82 spam \xe2\x94\x86 B84 \xe2\x94\x86 G96 \xe2\x94\x82\n\xe2\x94\x82 egg \xe2\x94\x86 C12 \xe2\x94\x86 L6 \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\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 … 将 DataFrame 写入 csv 文件时,我想附加到该文件,而不是覆盖它。
虽然 pandas DataFrame 具有可用模式.to_csv()参数的方法,因此允许将 DataFrame 附加到文件中,但 Polars DataFrame 写入方法似乎都没有该参数。
我想使用 package.json 读取 SQLite 数据库文件(database.sqlite)polars。我尝试以下操作未成功:
import sqlite3
import polars as pl
conn = sqlite3.connect('database.sqlite')
df = pl.read_sql("SELECT * from table_name", conn)
print(df)
Run Code Online (Sandbox Code Playgroud)
出现以下错误:
AttributeError: 'sqlite3.Connection' object has no attribute 'split'
Run Code Online (Sandbox Code Playgroud)
有什么建议么?
我有一个parquet文件(~1.5 GB),我想用polars. 生成的数据帧有 250k 行和 10 列。一栏里有大块的文本。
我刚刚开始使用极坐标,因为我听到了很多关于它的好消息。其中之一是它比 pandas 快得多。
这是我的问题:
数据帧的预处理相当慢,所以我开始与pandas. 我是否做错了什么,或者这个特定用例的极坐标只是速度较慢?如果是这样:有没有办法加快速度?
这是我的代码polars
import polars as pl
df = (pl.scan_parquet("folder/myfile.parquet")
.filter((pl.col("type")=="Urteil") | (pl.col("type")=="Beschluss"))
.collect()
)
df.head()
Run Code Online (Sandbox Code Playgroud)
整个代码大约需要1 分钟,而仅过滤部分大约需要13 秒。
我的代码在pandas:
import pandas as pd
df = (pd.read_parquet("folder/myfile.parquet")
.query("type == 'Urteil' | type == 'Beschluss'") )
df.head()
Run Code Online (Sandbox Code Playgroud)
整个代码也大约需要1 分钟,而仅查询部分就需要<1 秒。
数据框的 10 列具有以下类型:
如前所述:“ content”列存储大文本(1 到 20 页文本),我需要对其进行预处理,并且我猜存储方式不同。 …
在此示例中,在 columns 上["foo", "ham"],我希望删除第 1 行和第 4 行,因为它们与列表中的一对匹配
df = pl.DataFrame(
{
"foo": [1, 1, 2, 2, 3, 3, 4],
"bar": [6, 7, 8, 9, 10, 11, 12],
"ham": ["a", "b", "c", "d", "e", "f", "b"]
}
)
pairs = [(1,"b"),(3,"e"),(4,"g")]
Run Code Online (Sandbox Code Playgroud)
以下内容对我有用,但我认为当数据框和对列表很大时这会出现问题。
for a, b in pairs:
df = df.filter(~(pl.col('foo') == a) | ~(pl.col('ham') == b))
Run Code Online (Sandbox Code Playgroud)
我认为这是这个问题的 Pandas 实现Pandas: How to remove rows from a dataframe based on a tuples list made value in TWO columns?
我不确定 …
我是极地新手,不确定我是否.with_columns()正确使用。
这是我经常遇到的情况:有一个数据框,在其中.with_columns(),我对列应用一些操作。例如,我将一些日期转换为str类型date,然后想要计算开始日期和结束日期之间的持续时间。我将按如下方式实现这一点。
import polars as pl
pl.DataFrame(
{
"start": ["01.01.2019", "01.01.2020"],
"end": ["11.01.2019", "01.05.2020"],
}
).with_columns(
[
pl.col("start").str.strptime(pl.Date, fmt="%d.%m.%Y"),
pl.col("end").str.strptime(pl.Date, fmt="%d.%m.%Y"),
]
).with_columns(
[
(pl.col("end") - pl.col("start")).alias("duration"),
]
)
Run Code Online (Sandbox Code Playgroud)
首先,我转换两列,然后.with_columns()再次调用。
像这样更短的东西是行不通的:
pl.DataFrame(
{
"start": ["01.01.2019", "01.01.2020"],
"end": ["11.01.2019", "01.05.2020"],
}
).with_columns(
[
pl.col("start").str.strptime(pl.Date, fmt="%d.%m.%Y"),
pl.col("end").str.strptime(pl.Date, fmt="%d.%m.%Y"),
(pl.col("end") - pl.col("start")).alias("duration"),
]
)
Run Code Online (Sandbox Code Playgroud)
有没有办法避免调用.with_columns()两次并以更紧凑的方式编写?
假设我有一个像这样的 Polars 数据框:
df = pl.DataFrame({
'a': [0.3, 0.7, 0.5, 0.1, 0.9]
})
Run Code Online (Sandbox Code Playgroud)
现在我需要添加一个新列,根据列中的值'a'大于还是小于某个阈值来分配 1 或 0。在 Pandas 中我可以这样做:
import numpy as np
THRESHOLD = 0.5
df['new'] = np.where(df.a > THRESHOLD, 0, 1)
Run Code Online (Sandbox Code Playgroud)
我也可以在 Polars 中做一些非常类似的事情:
df = df.with_columns(
pl.lit(np.where(df.select('a').to_numpy() > THRESHOLD, 0, 1).ravel())
.alias('new')
)
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我确信在这里使用 NumPy 不是最佳实践。
我也尝试过类似的东西:
df = df.with_columns(
pl.lit(df.filter(pl.col('a') > THRESHOLD).select([0, 1]))
.alias('new')
)
Run Code Online (Sandbox Code Playgroud)
但使用这种语法我不断遇到以下错误:
DuplicateError Traceback (most recent call last)
Cell In[47], line 5
1 THRESHOLD = 0.5
2 DELAY_TOLERANCE = 10 …Run Code Online (Sandbox Code Playgroud) 在 Python 中对 Polars 数据框进行分组时,如何将单个列中的字符串值跨每个组中的行连接起来?
\n例如,给定以下 DataFrame:
\nimport polars as pl\n\ndf = pl.DataFrame(\n {\n "col1": ["a", "b", "a", "b", "c"],\n "col2": ["val1", "val2", "val1", "val3", "val3"]\n }\n)\nRun Code Online (Sandbox Code Playgroud)\n原始 df:
\nshape: (5, 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 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\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 a \xe2\x94\x86 val1 \xe2\x94\x82\n\xe2\x94\x82 b \xe2\x94\x86 val2 \xe2\x94\x82\n\xe2\x94\x82 a \xe2\x94\x86 val1 \xe2\x94\x82\n\xe2\x94\x82 b \xe2\x94\x86 val3 \xe2\x94\x82\n\xe2\x94\x82 c \xe2\x94\x86 val3 \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我想运行 groupby 操作,例如:
\nshape: (5, 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 …Run Code Online (Sandbox Code Playgroud) 我想解决极地地区熊猫所描述的问题。
特别是,考虑一个df1如下所示的 DataFrame:
c k l
A 1 a
A 2 b
B 2 a
C 2 a
C 2 d
Run Code Online (Sandbox Code Playgroud)
另一个 DataFrame 的名称df2如下:
c l
A b
C a
Run Code Online (Sandbox Code Playgroud)
在上面的问题中概述,我想过滤df1不在.df2
特别是,预期结果如下所示:
c k l
A 1 a
B 2 a
C 2 d
Run Code Online (Sandbox Code Playgroud)
对于上面链接的问题,得票最多的答案使用了从所需列构建的多重索引。我不明白这在极地是如何运作的。
当我使用时,我没有得到与极地熊猫相同的输出to_dict。
示例 1。
df = pd.DataFrame({
'column_1': [1, 2, 1,4,5],
'column_2': ['Alice', 'Bob', 'Alice','Tom', 'Tom'],
'column_3': ['Alice1', 'Bob', 'Alice2','Tom', 'Tom']
})
test = df.to_dict(orient='records')
print('PANDAS',test)
Run Code Online (Sandbox Code Playgroud)
带输出
[{'column_1': 1, 'column_2': 'Alice', 'column_3': 'Alice1'}, {'column_1': 2, 'column_2': 'Bob', 'column_3': 'Bob'}, {'column_1': 1, 'column_2': 'Alice', 'column_3': 'Alice2'}, {'column_1': 4, 'column_2': 'Tom', 'column_3': 'Tom'}, {'column_1': 5, 'column_2': 'Tom', 'column_3': 'Tom'}]
Run Code Online (Sandbox Code Playgroud)
示例 2.
dfPolars = pl.DataFrame({
'column_1': [1, 2, 1,4,5],
'column_2': ['Alice', 'Bob', 'Alice','Tom', 'Tom'],
'column_3': ['Alice1', 'Bob', 'Alice2','Tom', 'Tom']
})
testpolars = …Run Code Online (Sandbox Code Playgroud) python-polars ×10
python ×8
dataframe ×3
append ×1
dictionary ×1
pandas ×1
python-3.x ×1
sqlite ×1
writefile ×1