我有一个包含800项(行)的数据框架,每一行都位于不同的区域。这些地区包括:奥尔斯顿、波士顿、布莱顿、芬威、布鲁克林、剑桥、牛顿。
例如Pandas Dataframe:
area price location Bedroom
1 boston 3074 1 Devonshire Place 1
2 boston 3310 72 Staniford Street 2
3 allston 1825 1156 Commonwealth Avenue 1
4 cambridge 3895 39 Clinton Street 3
5 fenway 2325 98 Queensberry Street 1我尝试将这个数据帧的行随机分成三组:
”
”
每个项目/行只能分发一次。如果其中一个群体没有覆盖其中一些地区,这并不重要。如果C组只有‘波士顿和/或布莱顿’的项目,那就没问题,但C组不能有牛顿的项目。
我已经尝试过dataframe.sample()、np.split()、np.random.choice(),但是使用所有这些技术,行都会被复制。我计划编写一个循环,这样每次创建组时,随机选择的行都会有所不同。
知道怎么解决吗?
发布于 2021-04-19 04:49:32
这是这个特殊情况的代码。我不认为它能被广泛地概括。
根据C组允许的区域,样本0.1 * 800 = 80,标记为C组,然后从B组允许区域的未标记行中选择240,标记为B组。其余的必须在A组中。
import pandas as pd
import random
allowed = {
'A':"Allston Boston Brighton Fenway Brookline Cambridge Newton".split(),
'B':"Allston Boston Brighton Fenway".split(),
'C':"Boston Brighton Fenway".split()
}
weight = {
'A':0.6,
'B':0.3,
'C':0.1
}
# create random areas that meet the requirements 10% group C, 30% group B and 60% group A
rows = []
for area in random.choices(list(weight.keys()), weights=weight.values(), k=800):
rows.append(random.choice(allowed[area]))
# create a dummy data frame
df = pd.DataFrame({'areas':rows,
'price':[random.randrange(1000, 5000) for _ in range(len(rows))]})
# add a column for the group, set to '' to indicate unassigned
df['group'] = ['']*len(rows)
for group in 'CBA':
# Select rows that are not assigned to a group and that have areas that are
# allowed for the current group. Then randomly sample the selected rows.
xs = df[(df.group=='') & df['areas'].isin(allowed[group])].sample(n=int(len(rows)*weight[group]))
# Mark the sampled rows with the group
df.loc[xs.index,'group'] = group
# this just to see what's happening
print(group, len(xs))
print(df.head())
print()最终的结果是DataFrame有一个列‘组’,并根据给定的约束随机赋值。
https://stackoverflow.com/questions/67145174
复制相似问题