【问题标题】:Assigning Pandas categories to new number将 Pandas 类别分配给新号码
【发布时间】:2021-11-08 12:13:09
【问题描述】:
df["A"].value_counts()

(25.0, 38.0]      361594
(12.999, 25.0]    330552
(55.0, 218.0]     305439
(38.0, 55.0]      231683
Name: A, dtype: int64

我们有以下区间,每当有新数据点到来时,我需要映射到以下上述区间。我想要这样的东西。

def func_(x):
    if (x> 12.999) & (x< 25.0):
      return (12.999, 25.0]
    elif:
        for rest of bucket range 

【问题讨论】:

  • 能否提供一个构造函数来获取初始Series?
  • 所以基本上 pandas qcut 已用于识别范围。 pd.qcut()
  • 我明白了,但最好不用手动输入;)

标签: python pandas binning


【解决方案1】:

您可以按CategoricalIndex.categories生成的类别重用bins参数:

s = df["A"].value_counts()

print (pd.cut(df['new'], bins=s.index.categories))

【讨论】:

    猜你喜欢
    • 2019-12-11
    • 2017-09-05
    • 1970-01-01
    • 2010-10-31
    • 2019-02-06
    • 2021-11-11
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    相关资源
    最近更新 更多