I have a dataframe with the following information:
data = {'Code':['xxy','zzy', 'rrr'],
'Service':["23a","22c","21d"],
'Service2':["44e", "32c","85a"],
'Service3':["42e", "32z","8m"],
}
df = pd.DataFrame(data)
df
It looks like this table
and i would like it to look like this
please your help, thanks!
CodePudding user response:
df.set_index('Code').stack().reset_index()[['Code', 0]]
result:
Code 0
0 xxy 23a
1 xxy 44e
2 xxy 42e
3 zzy 22c
4 zzy 32c
5 zzy 32z
6 rrr 21d
7 rrr 85a
8 rrr 8m
if you want change column name, use following code(including full code):
df.set_index('Code').stack().reset_index()[['Code', 0]].rename(columns={0:'as you wish'})
Code as you wish
0 xxy 23a
1 xxy 44e
2 xxy 42e
3 zzy 22c
4 zzy 32c
5 zzy 32z
6 rrr 21d
7 rrr 85a
8 rrr 8m