I have a DataFrame like:
df = pd.DataFrame({'A': ['a', 'b', 'c'], 'B':['d', 'e', 'f'], 'C':[1,2,3], 'D':[4,5,6]})
A B C D
a d 1 4
b e 2 5
c f 3 6
I have to expand columns C and D treating A and B as keys. The output should look like:
A B key val
a d C 1
a d D 4
b e C 2
b e D 5
c f C 3
c f D 6
I have coded this as:
df_new = pd.DataFrame()
list_to_expand = ['C', 'D']
for index, row in df.iterrows():
for l in list_to_expand:
df_new = df_new.append(
pd.DataFrame({'A': row['A'],'B': row['B'], 'key': l, 'val': row[l]},
index=[0]))
I need to optimize my code in a vectorized format but couldn't find any function. Please note that the list of columns can increase i.e. ['C', 'D', 'E'...]. I am using python3 and pandas.