如何生成具有不同购买物品的 df [python]



我需要建立一个包含 100 张购买票的 df,购买的物品数量不同(1 到 15 件(;物品代码(1 到 100 件(。每一行都是一张票证。

我尝试在循环内使用 df.append,但 id 对我不起作用。

感谢您帮助解决此问题。

我的代码:

import pandas as pd
import random
random.seed(521) 
low =1 #the codes for the items are between 1 and 100
high =100
for i in range(100): #purchase sequences
    for j in range(1,16):#items per ticket
        seq1=random.sample(range(low, high), j)
        print(seq1)
print(seq1)

'

我的 df 必须看起来像(最多 5 个项目的示例(:

[注13]

[1, 40]

[33, 53, 92]

[23, 46, 13, 84]

[42, 35, 40, 71, 17]

[5]

[54, 95]

[23, 44, 87]

[17, 11, 50, 41]

[36, 94, 62, 1, 23]

#creates a dataframe with 1 column of arrays of random values
pd.DataFrame(pd.Series([[random.randint(1, 100) for __ in range(random.randint(1, 15))] for _ in range(100)]))
#creates a dataframe with 1 column for each values and NaNs for unfilled rows
pd.DataFrame([[random.randint(1, 100) for __ in range(random.randint(1, 15))] for _ in range(100)])

最新更新