AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-10855529

apostofes's questions

Martin Hope
apostofes
Asked: 2024-11-06 13:47:43 +0800 CST

在 polars 中 join_where 与 starts_with

  • 7

我有两个数据框,

df = pl.DataFrame({'url': ['https//abc.com', 'https//abcd.com', 'https//abcd.com/aaa', 'https//abc.com/abcd']})

conditions_df = pl.DataFrame({'url': ['https//abc.com', 'https//abcd.com', 'https//abcd.com/aaa', 'https//abc.com/aaa'], 'category': [['a'], ['b'], ['c'], ['d']]})

现在我想要一个 df,用于根据第二个 df 中以 url 开头的第一个匹配项为第一个 df 分配类别,即输出应该是,

网址 类别
https//abc.com ['一个']
https//abcd.com ['b']
https//abcd.com/aaa ['b'] - 这个以 https//abcd.com 开头,这是第一个匹配
https//abc.com/abcd ['a'] - 这个以 https//abc.com 开头,这是第一个匹配

目前有效的代码是这样的,

def add_category_column(df: pl.DataFrame, conditions_df) -> pl.DataFrame:
    
    # Initialize the category column with empty lists
    df = df.with_columns(pl.Series("category", [[] for _ in range(len(df))], dtype=pl.List(pl.String)))
    
    # Apply the conditions to populate the category column
    for row in conditions_df.iter_rows():
        url_start, category = row
        df = df.with_columns(
            pl.when(
                (pl.col("url").str.starts_with(url_start)) & (pl.col("category").list.len() == 0)
            )
            .then(pl.lit(category))
            .otherwise(pl.col("category"))
            .alias("category")
        )
    
    return df

但是有没有办法在不使用 for 循环的情况下实现相同的效果,我们可以在这里使用 join_where 吗,但在我的尝试中 join_where 对 starts_with 不起作用

python-polars
  • 3 个回答
  • 69 Views
Martin Hope
apostofes
Asked: 2024-10-01 17:11:24 +0800 CST

安全 ast 文字评估的纯极化版本

  • 6

我有这样的数据,

df = pl.DataFrame({'a': ["['b', 'c', 'd']"]})

我想将字符串转换为我使用的列表,

df = df.with_columns(a=pl.col('a').str.json_decode())

它给了我,

ComputeError: error inferring JSON: InternalError(TapeError) at character 1 (''')

然后我使用这个函数,

import ast
def safe_literal_eval(val):
    try:
        return ast.literal_eval(val)
    except (ValueError, SyntaxError):
        return val
df = df.with_columns(a=pl.col('a').map_elements(safe_literal_eval, return_dtype=pl.List(pl.String)))

并获得预期的输出,但是是否有纯极化方法可以实现相同的效果?

python
  • 2 个回答
  • 40 Views
Martin Hope
apostofes
Asked: 2024-06-02 22:28:39 +0800 CST

从极坐标列表中选择一组特定的字符串

  • 7
df = pl.DataFrame({'list_column': [['a.xml', 'b.xml', 'c', 'd'], ['e.xml', 'f.xml', 'g', 'h']]})

def func(x):
    return [y for y in x if '.xml' in y]

df.with_columns(pl.col('list_column').map_elements(func, return_dtype=pl.List(pl.String)))

有没有办法在不使用的情况下实现相同的目的map_elements?

python
  • 1 个回答
  • 28 Views
Martin Hope
apostofes
Asked: 2024-05-05 20:37:04 +0800 CST

获取极坐标中时间序列 df 中的第一个、最后一个出现位置

  • 8

如何获取时间序列 df 中特定列的第一次、最后一次出现 (>0),

shape: (4, 3)
┌────────────┬────────────┬────────────┐
│ date       ┆ column_one ┆ column_two │
│ ---        ┆ ---        ┆ ---        │
│ date       ┆ f64        ┆ i64        │
╞════════════╪════════════╪════════════╡
│ 2024-06-01 ┆ 0.0        ┆ 0          │
│ 2024-06-02 ┆ 0.0        ┆ 1          │
│ 2024-06-03 ┆ 1.0        ┆ 2          │
│ 2024-06-04 ┆ 1.2        ┆ 3          │
└────────────┴────────────┴────────────┘

并将其存储在单独的 df 中,

columns | first_appearance | last_appearance
column_one | 2024-06-03 | 2024-06-04
column_two | 2024-06-02 | 2024-06-04
python-polars
  • 1 个回答
  • 49 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve