Appending the values of multiple columns into one whilst retaining UIDs I have a table structured as below:

UID|ColA|ColB|ColC|ColD


I want to restructure so that all columns are combined (appended) into one, retaining the UID of each as below: 

UID|ColA
UID|ColB
UID|ColC
UID|ColD


I'm very new to Pandas and was wondering how this could be done? Thank you!
Assuming you already have a dataframe df, created as below:

In [1] : df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), 
                           columns=['a', 'b', 'c'])
In [2] : df                                                                                                                                                   
Out[2] : 
   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9


you could try doing this:

In [3] : df.melt("a").sort_values("a")                                                                                                                        
Out[3] : 
   a variable  value
0  1        b      2
3  1        c      3
1  4        b      5
4  4        c      6
2  7        b      8
5  7        c      9


where the sorting might be optional in your case. Also, you should swap "a" for the name of your column containing the UID values.

You can additionally drop the autogenerated variable column too, so the final solution would be:

In [4] : df.melt("a").sort_values("a").drop("variable", axis=1)                                                                                               
Out[4] : 
   a  value
0  1      2
3  1      3
1  4      5
4  4      6
2  7      8
5  7      9




From what I remember, the melt functionality is inspired on the melt function in the R programming language, where DataFrames are one of the core built-in data-types. It is really useful for plotting using the ggplot library (in R).