[英]Divide a group into n and add block numbers for each group in python
我有下表:
A欄 | B欄 |
---|---|
一個 | 12 |
乙 | 32 |
C | 44 |
D | 76 |
乙 | 99 |
F | 123 |
G | 65 |
H | 87 |
我 | 76 |
Ĵ | 231 |
ķ | 80 |
l | 55 |
米 | 27 |
n | 67 |
我想將此表划分為“n”(n = 4,此處為)組,並添加另一列與組名。 output 應如下所示:
A欄 | B欄 | C欄 |
---|---|---|
一個 | 12 | 1 |
乙 | 32 | 1 |
C | 44 | 1 |
D | 76 | 1 |
乙 | 99 | 2 |
F | 123 | 2 |
G | 65 | 2 |
H | 87 | 2 |
我 | 76 | 3 |
Ĵ | 231 | 3 |
ķ | 80 | 3 |
l | 55 | 4 |
米 | 27 | 4 |
n | 67 | 4 |
我這么努力是為了什么?
TGn = 4
idx = set(df.index // TGn)
treatment_groups = [i for i in range(1, n+1)]
df['columnC'] = (df.index // TGn).map(dict(zip(idx, treatment_groups)))
這不能正確拆分組,不確定我哪里出錯了。 我該如何糾正?
假設您的樣本大小正好除以 n(即sample_size%n
為 0):
import numpy as np
groups = range(1,n+1)
df['columnC'] = np.repeat(groups,int(len(df)/n))
如果您的樣本大小未完全除以 n(即sample_size%n
不為 0):
# Assigning the remaining rows to random groups
df['columnC'] = np.concatenate(
[np.repeat(groups,int(len(df)/n)),
np.random.randint(1, high=n, size=int(len(df)%n), dtype=int)])
# Assigning the remaining rows to group 'm'
df['columnC'] = np.concatenate(
[np.repeat(groups,int(len(df)/n)),
np.repeat([m],int(len(df)%n)), dtype=int)])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.