【问题标题】:pandas category column - why does .apply allow access to interval properties?pandas 类别列 - 为什么 .apply 允许访问区间属性?
【发布时间】:2021-06-03 17:22:43
【问题描述】:

this answer 中,我们可以看到 pd.cut() 产生一个类别类型的列,并且 .apply() 允许访问给定间隔值的属性。

例如,这是有效的:

df['b'] = df['a'].apply(lambda x: x.left)

但这不起作用:

df['b'] = df['a'].left

我不明白这是为什么。找不到好的解释。如果有人能解释一下,那就太好了。

【问题讨论】:

  • 熊猫系列没有左方法

标签: python pandas


【解决方案1】:

AmineBTG 对这个问题的评论回答了这个问题。

在下文中,.left 适用于系列,pd.series 没有 left 方法(或属性)

df['b'] = df['a'].left

在下文中, .apply() 逐行工作,并一次作用于系列值。所以 .left 被正确解释为每个 [interval] 值的属性。

df['b'] = df['a'].apply(lambda x: x.left)

【讨论】:

    猜你喜欢
    • 2017-02-21
    • 2020-03-12
    • 2014-10-17
    • 1970-01-01
    • 2014-03-16
    • 2019-06-03
    • 1970-01-01
    • 2022-11-15
    相关资源
    最近更新 更多