【问题标题】:Buffer has wrong number of dimensions (expected 1, got 2)缓冲区的维数错误(预期为 1,得到 2)
【发布时间】:2020-04-22 18:38:24
【问题描述】:

如果特定列的groupby 计数大于 2000,我需要选择数据帧的前 2000 行。如何在 python 中完成?以下是正确的做法吗

X_train[ 1:2000, X_train.groupby(['value']).count() > 2000].

我收到Buffer has wrong number of dimensions (expected 1, got 2)

如果特定列组按计数超过 2000,请帮助我选择 2000 行。

【问题讨论】:

  • 这是一个标准的pandas 问题,与machine-learningjupyter-notebook 无关 - 请不要向无关标签发送垃圾邮件(已删除)。

标签: python pandas


【解决方案1】:
df        = pd.DataFrame(np.random.randint(1, 10, 100).reshape((20,5))) 


col       = 0
lower_bnd = 2
n_rows    = 3

count     = df.groupby([col]).size()
selection = count[count > lower_bnd].index
dg        = df.loc[df[col].isin(selection)].iloc[:n_rows]

【讨论】:

  • 我想维护不超过此 groupby 计数的其他行。假设如果 df[col] 有 1,2,3 作为值, df.groupby['col'].count() of 2 单独超过 low_bound ,那么我只想将此条件应用于值为 2 的列。其他行应该为具有其他值的列维护。如何做到这一点?
  • 您能否构建一个工作示例并指定您要查找的输出?
猜你喜欢
  • 2011-12-28
  • 2022-08-07
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
  • 1970-01-01
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多