[英]Expanding pandas Data Frame rows based on number and group ID (Python 3).
I have been struggling with finding a way to expand/clone observation rows based on a pre-determined number and a grouping variable (id). 我一直在努力寻找一种基于预定数字和分组变量(id)扩展/克隆观察行的方法。 For context, here is an example data frame using pandas and numpy (python3). 对于上下文,这是使用pandas和numpy(python3)的示例数据帧。
df = pd.DataFrame([[1, 15], [2, 20]], columns = ['id', 'num'])
df
Out[54]:
id num
0 1 15
1 2 20
I want to expand/clone the rows by the number given in the "num" variable based on their ID group. 我想通过基于其ID组的“ num”变量中给出的数字来扩展/克隆行。 In this case, I would want 15 rows for id = 1 and 20 rows for id = 2. This is probably an easy question, but I am struggling to make this work. 在这种情况下,我想要id = 1的15行和id = 2的20行。这可能是一个简单的问题,但是我正在努力进行这项工作。 I've been messing around with reindex and np.repeat, but the conceptual pieces are not fitting together for me. 我一直在搞乱reindex和np.repeat,但是概念上的部分对我来说不太合适。
In R, I used the expandRows function found in the splitstackshape package, which would look something like this: 在R中,我使用了splitstackshape包中的expandRows函数,它看起来像这样:
library(splitstackshape)
df <- data.frame(id = c(1, 2), num = c(15, 20))
df
id num
1 1 15
2 2 20
df2 <- expandRows(df, "num", drop = FALSE)
df2
id num
1 1 15
1.1 1 15
1.2 1 15
1.3 1 15
1.4 1 15
1.5 1 15
1.6 1 15
1.7 1 15
1.8 1 15
1.9 1 15
1.10 1 15
1.11 1 15
1.12 1 15
1.13 1 15
1.14 1 15
2 2 20
2.1 2 20
2.2 2 20
2.3 2 20
2.4 2 20
2.5 2 20
2.6 2 20
2.7 2 20
2.8 2 20
2.9 2 20
2.10 2 20
2.11 2 20
2.12 2 20
2.13 2 20
2.14 2 20
2.15 2 20
2.16 2 20
2.17 2 20
2.18 2 20
2.19 2 20
Again, sorry if this is a stupid question and thanks in advance for any help. 再次,如果这是一个愚蠢的问题,对不起,请先感谢您的帮助。
I can't replicate your index, but I can replicate your values, using np.repeat
, quite easily in fact. 我无法复制您的索引,但实际上可以很容易地使用np.repeat
复制您的值。
v = df.values
df = pd.DataFrame(v.repeat(v[:, -1], axis=0), columns=df.columns)
If you want the exact index (although I can't see why you'd need to), you'd need a groupby
operation - 如果您想要确切的索引(尽管我看不到为什么groupby
),则需要进行groupby
操作-
def f(x):
return x.astype(str) + '.' + np.arange(len(x)).astype(str)
idx = df.groupby('id').id.apply(f).values
Assign idx
to df
's index - 将idx
分配给df
的索引-
df.index = idx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.