【发布时间】:2021-04-29 04:52:51
【问题描述】:
我正在尝试将 while 语句应用于我的代码,以便运行它,直到下面列表中的所有元素(在 Check 列中)都在 Source 列中。
到目前为止,我的代码是这样的:
while set_condition: # to set the condition
newCol = pd.Series(list(set(df['Check']) - set(df['Source']))) # this check for elements which are not currently included in the column Source
newList1 = newCol.apply(lambda x: my_function(x)) # this function should generate the lists n Check -> this explains why I need to create a while statement
df = df.append(pd.DataFrame(dict('Source'=newCol, 'Check'=newList1)), ignore_index=True) # append the results in the new column
df = df.explode('Check')
我会给你一个例子来说明这个过程以及my_function是如何工作的:假设我有我的初始数据集
Source Check
mouse [dog, horse, cat]
horse [mouse, elephant]
tiger []
elephant [horse, bird]
在分解Check 列并将结果附加到Source 之后,我将拥有
Source Check
mouse [dog, horse, cat]
horse [mouse, elephant]
tiger []
elephant [horse, bird]
dog [] # this will be filled in after applying the function
cat [] # this will be filled in after applying the function
bird [] # this will be filled in after applying the function
在应用函数之前,列表中的每个元素都应该添加到 Source 列中。 当我应用这个函数时,我填充了其他元素的列表;所以,例如我可以有
Source Check
mouse [dog, horse, cat]
horse [mouse, elephant]
tiger []
elephant [horse, bird]
dog [mouse, fish] # they are filled in
cat [mouse]
bird [elephant, penguin]
fish [dog]
由于fish 和penguin 不在Source 中,我需要再次运行代码以获得预期的输出(列表中的所有元素都已经在Source 列中):
Source Check
mouse [dog, horse, cat]
horse [mouse, elephant]
tiger []
elephant [horse, bird]
dog [mouse, fish]
cat [mouse]
bird [elephant, penguin]
fish [dog]
penguin [bird]
因为dog 和bird 都已经在Source 中,所以我不需要再次应用该函数,因为所有列表都填充了源列中已经存在的元素。代码可以停止运行。
我想做的是在列表中的所有元素都在列 Source 中并应用该函数填充所有列表时停止循环/循环。
感谢您提供的所有帮助。
【问题讨论】:
-
我不明白你的问题。在“应用功能后,我会有例子”这一步中,企鹅来自哪里?为什么狗与老鼠和鱼有关?不知道自己想做什么就很难回答。
-
@ApplePie 这很简单,但我同意我不是很清楚;)我在检查列中有列表。我想在 Source 列中包含所有元素(没有重复项)。每当我在 Source 中有新元素时,my_function 都会生成它们在 Check 中的相关列表。我想运行(然后停止)该过程,直到所有列表都包含已经在源中的元素。我试图在帖子中解释得更好
-
如果您可以提供最小的可重现输入,这将有所帮助。谢谢。
-
嗨,Akshay,不幸的是,我不知道如何总结从源列表中随机生成元素的函数,包括新元素。您可以考虑从 1 到 100 的数字(假设 Source 列中只有奇数)以及在 Check 列的列表中随机生成奇数和偶数的函数。由于在 Source 列中只有奇数(假设我们有 5 行)并且第一个列表包含奇数和偶数,因此我需要在 Source 列中添加偶数。他们还没有任何列表,因为我必须将函数重新应用到他们每个人。
-
一旦将函数应用到包括偶数在内的每一行,我就会有奇数和/或偶数的新列表。需要添加尚未包含在“源”列中的所有数字。然后我应该重新应用该函数来生成新列表。当列表中的所有数字都正确添加到源列中并且全部带有列表时,我将需要停止该过程。我想提供更多帮助,但我只能解释函数是如何工作的,以便使循环应该做的事情更简单。对于那个很抱歉。很高兴回答您可能遇到的所有问题
标签: python pandas while-loop