Length Of Values Does Not Match Length Of Index During A For Loop
I have a dataset (df) like this Name1 Name2 Score John NaN NaN Patty NaN NaN where Name2 and Score are initialized to NaN. Some data, like the following name2_list=[[Chris
Solution 1:
I think it is not a good idea to use pandas for this kind of problem. If you are fine with plain python for intermediate steps, you could do this:
import pandas as pd
defget_links(source_name):
"""Dummy function with data from OP.
Note that it processes one name at a time instead of batch like in OP.
"""
dummy_output = {
'John': (
['Chris', 'Luke', 'Martin'],
[1, 2, 4]
),
'Patty': (
['Martin'],
[9]
),
'Chris': (
['Patty'],
[9]
),
'Luke': (
['Martin'],
[1]
),
'Martin': (
['Laura'],
[3]
),
'Laura': (
['John'],
[3]
)
}
target_names, scores = dummy_output.get(source_name, ([], []))
return [
{'name1': source_name, 'name2': target_name, 'score': score}
for target_name, score inzip(target_names, scores)
]
todo = ['John', 'Patty']
seen = set(todo)
data = []
while todo:
source_name = todo.pop(0) # If you don't care about order can .pop() to get last element (more efficient)# get new data
new_data = get_links(source_name)
data += new_data
# add new names to queue if we haven't seen them before
new_names = set([row['name2'] for row in new_data]).difference(seen)
seen.update(new_names)
todo += list(new_names)
pd.DataFrame(data)
Output:
name1 name2 score
0 John Chris 1
1 John Luke 2
2 John Martin 4
3 Patty Martin 9
4 Chris Patty 9
5 Luke Martin 1
6 Martin Laura 3
7 Laura John 3
Post a Comment for "Length Of Values Does Not Match Length Of Index During A For Loop"