Assuming the following DataFrame:
key.0 key.1 key.2 topic
1 abc def ghi 8
2 xab xcd xef 9
How can I combine the values of all the key.* columns into a single column 'key', that's associated with the topic value corresponding to the key.* columns? This is the result I want:
topic key
1 8 abc
2 8 def
3 8 ghi
4 9 xab
5 9 xcd
6 9 xef
Note that the number of key.N columns is variable on some external N.
OK , cause one of the current answer is mark as duplicated of this question, I will answer here.
By Using
wide_to_long
After trying various ways, I find the following is more or less intuitive, provided
stack
's magic is understood:The resulting dataframe is as required:
You may want to print intermediary results to understand the process in full. If you don't mind having more columns than needed, the key steps are
set_index('topic')
,stack()
andreset_index(name='key')
.You can melt your dataframe:
It also gives you the source of the key.
From
v0.20
,melt
is a first class function of thepd.DataFrame
class: