在R数据表中,有一种方法可以结合最终向量的长度来重复向量.例如,我创建了名为‘PERIOD’的变量,它重复了向量Q.

R代码:

require(data.table)
q = c(1:3)
test = data.table(IDx = c(rep('42', 5) , rep('76', 3), rep('43', 3), rep('5', 2)),
                  IDy = c(rep('A', 5) , rep('A', 3), rep('B', 3) , rep('C',2)))
test[, period := rep(q, length.out = .N), by =c('IDx','IDy')]

   IDx IDy period
 1:  42   A      1
 2:  42   A      2
 3:  42   A      3
 4:  42   A      1
 5:  42   A      2
 6:  76   A      1
 7:  76   A      2
 8:  76   A      3
 9:  43   B      1
10:  43   B      2

我正试图用Python复制这个函数,但我有点卡住了.Cumcount函数只能通过考虑一旦达到最后一个索引就应该重新开始的序列Q来apply.

q = [1,2,3]
valuesX = ['42'] * 5 + ['76'] * 3 + ['43'] * 3 + ['5'] * 2 
valuesY = ['A'] * 5 + ['A'] * 3 + ['B'] * 3 + ['C'] * 2
test = pd.DataFrame({'IDx':valuesX,
                    'IDy':valuesY})

print(test.groupby(['IDx','IDy']).cumcount()+1)

试图接近,

def repeat(seq, ind):
    length = len(ind.index)
    print(length)
    multiple, remainder = divmod(length, len(seq))
    test['t'] = seq * multiple + seq[:remainder]

print(test.groupby(['IDx']).apply(lambda x: repeat(q, x))) 

推荐答案

groupby.cumcountmod组合在一起,然后使用NumPy索引(您也可以将值map):

import numpy as np

s = test.groupby(['IDx', 'IDy']).cumcount().mod(len(q))

test['period'] = np.array(q)[s]

输出:

   IDx IDy  period
0   42   A       1
1   42   A       2
2   42   A       3
3   42   A       1
4   42   A       2
5   76   A       1
6   76   A       2
7   76   A       3
8   43   B       1
9   43   B       2
10  43   B       3
11   5   C       1
12   5   C       2

Python-3.x相关问答推荐

为什么vs code返回错误—LocaleError:int对象没有属性where,但相同的代码运行在Google Colab上没有任何问题''''

在多个测试中维护和报告变量

Python多处理池:缺少一个进程

Python webdrivermanager 和 Chrome 115.0 的 URL https://chromedriver.storage.googleapis.com/LATEST_RELEASE_115.0.5790 错误没有此类驱动程序

两个 y 轴在零处对齐的 plotly barplot

使用 multiprocessing 处理图像

提高时间复杂度的一些建议

缺失时推断的数据类可选字段

使用 OpenCV 从图像中减go 一条线

考虑到Pandas 系列中的不同索引,如何正确估计两列的百分比变化? Python相关

通过附加/包含多个列表来创建 nDimensional 列表

是否可以将多个 if 转换为数组?

使用正则表达式捕获组解析地址

在 Django 中执行 JSONRenderer.render(serialized_student_data.data) 时遇到问题

如何在 VSCode 的在 Cloud Run Emulator 上运行/调试构建设置中添加 SQL 连接

例外:使用 Pyinstaller 时找不到 PyQt5 插件目录,尽管 PyQt5 甚至没有被使用

Python 3.5 中编码 utf-8 和 utf8 的区别

__new__ 方法给出错误 object.__new__() 只接受一个参数(要实例化的类型)

如何将 cv2.imread 匹配到 keras image.img_load 输出

导入父目录进行简要测试