Python 根据列值展开数据集

Python 根据列值展开数据集,python,pandas,numpy,Python,Pandas,Numpy,我有一个数据帧df1: Date_1 Date_2 i_count c_book 01/09/2019 02/08/2019 2 204 01/09/2019 03/08/2019 2 211 01/09/2019 04/08/2019 2 218 01/09/2019 05/08/2019 2 226 01/09/2019 06/08/2019 2 234 01/09/2019 07/08/2

我有一个数据帧df1:

Date_1     Date_2       i_count c_book
01/09/2019  02/08/2019  2       204
01/09/2019  03/08/2019  2       211
01/09/2019  04/08/2019  2       218
01/09/2019  05/08/2019  2       226
01/09/2019  06/08/2019  2       234
01/09/2019  07/08/2019  2       242
01/09/2019  08/08/2019  2       251
01/09/2019  09/08/2019  2       259
01/09/2019  10/08/2019  3       269
01/09/2019  11/08/2019  3       278
01/09/2019  12/08/2019  3       288
01/09/2019  13/08/2019  3       298
01/09/2019  14/08/2019  3       308
01/09/2019  15/08/2019  3       319
01/09/2019  16/08/2019  4       330
01/09/2019  17/08/2019  4       342
01/09/2019  18/08/2019  4       354
01/09/2019  19/08/2019  4       366
01/09/2019  20/08/2019  4       379
01/09/2019  21/08/2019  5       392
01/09/2019  22/08/2019  5       406
01/09/2019  23/08/2019  6       420
01/09/2019  24/08/2019  6       435
01/09/2019  25/08/2019  7       450
01/09/2019  26/08/2019  8       466
01/09/2019  27/08/2019  9       483
01/09/2019  28/08/2019  10      500
01/09/2019  29/08/2019  11      517
01/09/2019  30/08/2019  12      535
01/09/2019  31/08/2019  14      554
我想根据
I\u计数
扩展数据集
i_count
是要复制的行数。因此,假设
i_count=2
意味着需要为同一行复制两行

另外,我想创建一个新列
c_book_I
,这样
c_book
应该在数据集中的条目中进行划分。例如,如果
i_count=2
,则表示新数据框应该有2个条目,
c_book_i
应该有2个条目,以便
sum(c_book_i)=c_book
。最后一个限制是,我希望在所有情况下都有
c\u book\u I>10

到目前为止:

def f(x):
    i = np.random.random(len(x))
    j = i/sum(i) * x
    return j

joined_df2 = df1.reindex(df1.index.repeat(df1['i_count']))
joined_df2['c_book_i'] = joined_df2.groupby(['Date_1','Date_2'])['c_book'].transform(f)
这为我提供了相同的功能,但没有检查c_book是否应大于10。很多值小于10。

有人能帮忙吗

谢谢

那么:

def distribute_randomly(array):

    # This is the minimum to give each:
    minimum = 10

    # This means we have to reserve this amount:
    min_value_sum = len(array)*minimum

    # The rest we can distribute:
    to_distribute = array.sum() - min_value_sum

    # Get random values that all sum up to 1:
    random_values = numpy.random.rand(len(array))
    random_values = random_values/random_values.sum()

    # Return the minimum + a part of what is left to distribute
    return random_values*to_distribute + minimum

# Expand rows based on length of i_count:
df1 = df1.join(df1['i_count'].apply(lambda x: range(x)).explode().rename('dummy'))

# transform cbook_ to randomize
df1['c_book_2'] = df1.groupby('i_count')['c_book'].transform(distribute_randomly)

# Finally make sure they are not below 10:
df1['c_book_i'] = df1['c_book_2'].where(df1['c_book_2']>10, 10)

# If needed:
df1 = df1.reset_index()
编辑:增加了“随机”分配功能。



它是如何工作的。让总数为12,我们想把它分成4部分,最少2部分。我们通过步骤2=>
[2,4,6,8,10]
得到了从2到12-2的范围。然后获取任意3个数字,例如
2,4,8
并添加边框,因此,
[0,2,4,8,12]
。现在,该列表中项目之间的差异将得到总和12(边界之间的差异),并且它们之间的任何差异都不能小于2

谢谢您的回答,但是,我不想做x/len(x)我想把它设置为随机的,这样c_book_iIs的值就不同了,不一样了。有任何方法可以随机分配它,而不是执行x/len(x)确定,但您需要某种形式的随机分配。我不确定您的代码是否会考虑a(date_1,date_2)的总和(c_book_I)等于(日期1,日期2)的c_b。同样,任何分布在这个意义上都是有效的,因为分析是在聚合的c_b上进行的,并且存在的值满足这一点。所以我对分布不是很挑剔。如果你能在你的答案中提供一些分发格式,那就太好了。根据我的理解,lambda只需要更改。我也无法理解第二步的解释
cbook\uu
没有出现在我们这里。我没有完全按照您想要的,但我已经试着理解了。请您解释一下,为什么使用带分隔符的排序。如果您能向我解释一下,我将不胜感激。您看到链接了吗?我在应用此函数时也遇到了此错误:
TypeError:'numpy.float64'对象不能解释为整数
您能看一下错误并基于此建议更改吗?当然,请编辑答案一旦你得到时间,我得到的错误,因为我在前面的评论张贴。任何帮助都将不胜感激。谢谢。
def f(x):
    total = x.iloc[0].astype(int)
    minimum = 10
    dividers = sorted(random.sample(range(minimum, total-minimum, minimum), len(x) - 1))
    return [a - b for a, b in zip(dividers + [total], [0] + dividers)]