【发布时间】:2021-04-22 01:37:38
【问题描述】:
我有一个 while 循环,它从列表中随机返回三个项目。然后再次执行相同操作,直到列表为空。我想在 pandas 数据框中收集所有这 16 行。
from random import randint
colors = ['Pink', 'Purple', 'Green', 'Skyblue', 'Blue', 'Grey'] * 8
while colors:
lst = [colors.pop(randint(0, len(colors) - 1)) for _ in range(3)]
print(lst)
我的目标是从 while 循环中返回一个四列 16 行的 pandas 数据框。
pd.DataFrame(index=np.arange(1,17), columns = ["Store", "Bar 1", "Bar 2", "Bar 3"])
我不知道如何让 while 循环返回这样的数据帧。任何帮助将不胜感激。
【问题讨论】:
标签: python pandas while-loop