I have a dataframe with lots of key/value columns whereas the keys and values are separated columns.
import pandas as pd
values = [['John', 'somekey1', 'somevalue1', 'somekey2', 'somevalue2']]
df = pd.DataFrame(values, columns=['name', 'key1', 'value1', 'key2', 'value2'])
Remark: The original data would have more preceding columns and not just the name. And it has more than just two key/value columns.
What I want to achieve is having a result like this:
values = [
['John', 'somekey1', 'somevalue1'],
['John', 'somekey2', 'somevalue2']
]
df = pd.DataFrame(values, columns=['name', 'key', 'value'])
There I was thinking to join all key/value columns into a list or dictionary and than explode that list/dict. I found this nice posting on pd.melt but my problem is, that I don't know the exact id_var columns upfront. Therefore I tried pd.Series.stack, which gave me the correct result for the key/value column, but missing the other columns from the original data. Any idea? Here's what I tried:
# generates: [(somekey1, somevalue1), (somekey2, somevalue2)]
df['pairs'] = df.apply(lambda row: [(row['key1'],row['value1']), (row['key2'], row['value2'])], axis=1)
# unstacks the list, but drops all other columns
df['pairs'].apply(pd.Series).stack().reset_index(drop=True).to_frame('pairs')