将列表中的元素添加到数据帧中的随机行

2024-05-12 00:46:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从一些食谱书中创建一个“随机膳食选择器”。目标是用它来选择我一周中每天的晚餐。我已经为“膳食”创建了一个字典,并将其转换为一个单词,为“天”创建了一个列表。以下是一个示例:

meals = {'Recipe': ['Chicken Kebab', 'Chicken Balti', 'Chicken Stir Fry', 'Chicken Curry', 'Cola Chicken', 'Chicken Fajita Pie',
                    'Chicken in Black Bean Sauce', 'Stuffed Meatballs', 'Pesto Pasta'],
         'Book': ['1', '2', '1', '3', '1', '1', '3', '1', '1'],
         'Page': ['48', '50', '52', '58', '72', '74', '80', '87', '108'],
         'Category': ['Normal', 'Curry', 'Asian', 'Curry', 'Normal', 'Normal', 'Asian', 'Pasta', 'Pasta']}

df = pd.DataFrame(meals)

days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

我想做的是,随机地将列表中的每个元素应用到数据框中的一个新的“day”列中,这样就可以选择我每天制作的食谱。我该怎么做呢?接下来,我想创建一些规则,例如,只能选择一个类别的“咖喱”,因此,任何与此相关的帮助都将非常好!对Python来说非常陌生,因此非常感谢您提供的所有帮助


Tags: 示例目标列表字典选择器单词normal食谱
2条回答

你的伙食比现在多,而且没有解释你将如何使用它们,所以我临时为你准备了

meals = {'Recipe': ['Chicken Kebab', 'Chicken Balti', 'Chicken Stir Fry', 'Chicken Curry', 'Cola Chicken', 'Chicken Fajita Pie',
                    'Chicken in Black Bean Sauce', 'Stuffed Meatballs', 'Pesto Pasta'],
         'Book': ['1', '2', '1', '3', '1', '1', '3', '1', '1'],
         'Page': ['48', '50', '52', '58', '72', '74', '80', '87', '108'],
         'Category': ['Normal', 'Curry', 'Asian', 'Curry', 'Normal', 'Normal', 'Asian', 'Pasta', 'Pasta']}

df = pd.DataFrame(meals)

days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

print (df)
mealdays = []
while len(meals['Recipe']) != len(mealdays):
    day = random.choice(days)
    #put an if statement here for your future condition. i.e if Curry already chosen etc.
    mealdays.append(day)
    #the below is to make sure you have atleast one meal per day, and then if you have more than 7 recipes which you do, you will have another set of days to chose from
    days.remove(day)
    if len(days) == 0:
        days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

df['Days'] = mealdays
print (df)

您的条件应该使用注释掉的if语句来实现

然后我添加了另一个while循环,以确保食谱的数量=天数+空白的数量/nextweeks/您选择的用法

删除了if len(days)=0,因为现在这无关紧要

添加了日期列表的洗牌。(这一点很重要,因为“下周”的值是重复的),因此第一个食谱被选为“下周”的概率更高,因为“下周”的数量比周一/周三等多(请注释掉shuffle以查看)

meals = {'Recipe': ['Chicken Kebab', 'Chicken Balti', 'Chicken Stir Fry', 'Chicken Curry', 'Cola Chicken', 'Chicken Fajita Pie',
                    'Chicken in Black Bean Sauce', 'Stuffed Meatballs', 'Pesto Pasta'],
         'Book': ['1', '2', '1', '3', '1', '1', '3', '1', '1'],
         'Page': ['48', '50', '52', '58', '72', '74', '80', '87', '108'],
         'Category': ['Normal', 'Curry', 'Asian', 'Curry', 'Normal', 'Normal', 'Asian', 'Pasta', 'Pasta']}

df = pd.DataFrame(meals)

days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

print (df)
mealdays = []
while len(meals['Recipe']) != len(days):
    days.append("Next Week") #"Blank", " ", "Nothing"
while len(meals['Recipe']) != len(mealdays):
    day = random.choice(days)
    #put an if statement here for your future condition. i.e if Curry already chosen etc.
    mealdays.append(day)
    #the below is to make sure you have atleast one meal per day, and then if you have more than 7 recipes which you do, you will have another set of days to chose from
    days.remove(day)


random.shuffle(days)

df['Days'] = mealdays
print (df)

相关问题 更多 >