I have a DataFrame like this:
data = {'id': [112, 114, 221, 262, 299, 300], 'parent_id': [300, 262, 558, 221, 560, 299], 'name': ['zona1', 'zona2', 'Activo1', 'zona3', 'Activo2', 'zona4']}
pd.DataFrame.from_dict(data)
My goal is to create another column with the name of the parent_id that matchs with a list of 6 items:
list_names_act = [558, 559, 560, 561, 562, 563]
So, I get the first item of the column id = 112. Since 112 is not found in list_names_act I have to get the item at the same position of the column parent_id = 300 and do the same comprobation. 300 is not in list_names_act so I must search it in column id and check if the item in the column parent_id (299) in the same position is in list_names_act. 299 is not in list_names_act so I must get that item again and search it in id and, again, check if the item in parent_id is in list_names_act. In this last iteration, the value does belong to list_names_act so I must get the value of the column name in that position and replicate that name to all the values that were needed to get there including the last one. My output would be this:
data = {'id': [112, 114, 221, 262, 299, 300],
'parent_id': [300, 262, 558, 221, 560, 299],
'name': ['zona1', 'zona2', 'Activo1', 'zona3', 'Activo2', 'zona4'],
'name_activo': ['Activo2', 'Activo1', 'Activo1', 'Activo1', 'Activo2', 'Activo2' ]}
pd.DataFrame.from_dict(data)
I´ve tried with for loops but I´m only capable of filling 42 regs and I really don't know how to handle the change in the value that I'm looking for:
nom_activo_grande = []
for i in range(len(locacion_merge['id_x'])):
if locacion_merge['id_x'][i] in lista_tipos:
nom_activo_grande.append(locacion_merge['name'][i])
elif locacion_merge['parent_id'][i] in lista_tipos:
nom_activo_grande.append(locacion_merge['name'][i])
elif locacion_merge['parent_id'][i] not in lista_tipos:
for j in range(len(locacion_merge['id_x'])):
if locacion_merge['parent_id'][i] == locacion_merge['id_x'][j]:
else: nom_activo_grande.append(0)
thanks so much
