I have a pandas dataframe:
| col1 | heading |
|--------|---------|
|heading1| true |
|abc | false |
|efg | false |
|hij | false |
|heading2| true |
|klm | false |
|... | false |
This data is actually "sequential" and I would like to transform it to this structure:
| col1 | Parent |
|---------------------
|heading1| heading1 |
|abc | heading1 |
|efg | heading1 |
|hij | heading1 |
|heading2| heading2 |
|klm | heading2 |
|... | headingN |
I have +10M rows so this method takes too long:
df['Parent'] = df['col1']
for index, row in df.iterrows():
if row['heading']:
current = row['col1']
else:
row.loc[index, 'Parent'] = current
Do you have any advice on a faster process?