You don't need a loop to do it. With datar, you can achieve it with dplyr-like syntax:
>>> from datar.all import *
>>>
>>> # Create the df
>>> df = tribble(
... f.A, f.B, f.C, f.D, f.E, f.F, f.Cost,
... 0, 1, 1, 0, 0, 0, 10,
... 0, 0, 1, 0, 1, 0, 3,
... 1, 0, 0, 0, 0, 1, 5,
... 0, 1, 0, 1, 0, 0, 7,
... )
>>> df
A B C D E F Cost
<int64> <int64> <int64> <int64> <int64> <int64> <int64>
0 0 1 1 0 0 0 10
1 0 0 1 0 1 0 3
2 1 0 0 0 0 1 5
3 0 1 0 1 0 0 7
>>> # replace value with column names
>>> df = df >> mutate(across(f[1:6], lambda x: if_else(x, x.name, "")))
>>> df
A B C D E F Cost
<object> <object> <object> <object> <object> <object> <int64>
0 B C 10
1 C E 3
2 A F 5
3 B D 7
>>> # unite the columns
>>> df = df >> unite('col', f[1:6], sep="")
>>> df
col Cost
<object> <int64>
0 BC 10
1 CE 3
2 AF 5
3 BD 7
>>> # reshape the result
>>> df >> column_to_rownames(f.col) >> t()
BC CE AF BD
<int64> <int64> <int64> <int64>
Cost 10 3 5 7
Disclaimer: I am the author of the datar package.