我有这样一个数据集(df
)
Name1 Name2 Score
John NaN NaN
Patty NaN NaN
其中Name2
和Score
被初始化为NaN
。一些数据,如下所示
name2_list=[[Chris, Luke, Martin], [Martin]]
score_list=[[1,2,4],[3],[]]
在函数的每个循环处生成。这两个列表需要添加到mydf
中的Name2
和Score
列中,以便:
Name1 Name2 Score
John [Chris, Luke, Martin] [1,2,4]
Patty [Martin] [3]
然后,因为我希望在Name2
和Score
中有值而不是列表,所以我展开数据集:
Name1 Name2 Name3
John Chris 1
John Luke 2
John Martin 4
Patty Martin 3
我的目标是让Name2
中的所有值都在Name1
中。但是,正如我所提到的,我有一个如下工作的函数:对于Name2
中的每个元素,而不是Name1
中的每个元素,它检查是否还有其他值。生成的这些值与name2_list
和score_list
的值类似。
例如,假设在第二次迭代中,Chris
从函数生成的值等于[Patty]
和9
Luke
有值[Martin]
和1
Martin
有值[Laura]
和3
。然后,我需要将这些值再次添加到我的原始df
,以便(在分解之前)具有
Name1 Name2 Score
John Chris 1
John Luke 2
John Martin 4
Patty Martin 3
Chris Patty 9
Luke Martin 1
Martin Laura 3
只有一个值Laura
不在Name1
中,因此我需要再次运行该函数:如果输出已经包含在Name1
中,则我的循环停止,并获得最终的数据集;否则,我需要重新运行函数,看看是否需要更多的循环。
为了缩短本例中的时间,我们假设运行函数后Laura
的值为John
,3
John
已经在Name1
中,因此我不需要重新运行该函数
我所做的工作如下:
name2_list, score_list = [],[] # Initialize lists. These two lists need to store outputs from my function
name2 = df['name2'] # Append new name2 to this list as I iterate
name1 = df['name1'] # Append new name1 to this list as I iterate
distinct_name1 = set(name1) # distinct name1. I need this to calculate the difference
diff = set(name2) ^ distinct_name1 # This calculates the difference. I need to iterate until this list is empty, i.e., when len(diff)=0
if df.Name2.isnull().all(): # this condition is to start the process. At the beginning I have only values in Name1. No values in Name2
if len(diff)>0: # in the example the difference is 2 at the beginning, i.e., John and Patty; at the second round 3 (Chris, Luke, Martin); at the third round is only for Laura. There is no fourth round
for x in diff: # I run it first for John, then for Patty
collected_data = fun(df, diff) # I will explain below what this function does and how it looks like
df = df.apply(pd.Series.explode) # in this step I explode the dataset
name2 = df['Name2'] # I am updating the list of values in Name2 to calculate the difference after each iteration.
name1 = df['Name1'] # I am updating the list of values in Name1 to calculate the difference after each iteration.
distinct_name1 = set(name1) # calculate the new difference
diff = filter(None, (set(name2) ^ distinct_name1) ) # calculate the new difference. Iterate until this is empty
当在函数
中考虑此步骤^ {< CD34>}时发生错误---> 33 df['Name2'] = name2_list
说:
ValueError: Length of values (6) does not match length of index (8).
(圆括号内的值可能不同于通过使用此示例获得的值)
我的函数目前不关心数据帧中有多少行,它正在创建不同长度的新列表。我需要找到一种方法来调和这一点。我正在调试,我可以确认错误来自函数中的df['Name2'] = name2_list
。我能够正确打印新名称2值的列表,但不能打印列。
也许,一个可能的解决方案是在for
循环之外构建一次df,但我需要分解df['Name2']
并构建用于存储web结果的列表
我认为用熊猫来解决这类问题不是一个好主意。如果您对普通python的中间步骤没有问题,可以这样做:
输出:
相关问题 更多 >
编程相关推荐