Это более эффективное решение.Я разбиваю шаги:
# Compute the Value column with `agg`.
v = df.astype(str).agg(','.join)
# Compute the Input column with `df.columns.str.cat`
v.index = [df.columns.str.cat(sep='/')] * len(v)
# Reset the index.
v.rename_axis('Input').to_frame('Value').reset_index()
Input Value
0 Wash_Month/Wash_Day 3,2
1 Wash_Month/Wash_Day 4,3
Альтернатива (медленнее).Немного измените ваши данные с помощью stack
:
v = df.stack().astype(str).reset_index(level=1)
v.columns = ['Input', 'Value']
print(v)
Input Value
0 Wash_Month 3
0 Wash_Day 2
1 Wash_Month 4
1 Wash_Day 3
Посмотрите на индекс (!).Теперь звоните groupby
и agg
:
v.groupby(level=0).agg({'Input': '/'.join, 'Value':','.join})
Input Value
0 Wash_Month/Wash_Day 3,2
1 Wash_Month/Wash_Day 4,3