-3

This is my initial data:

name  s1 s2 s3 s4 s5
a     sd fg fd wer er
b     df rf qw NaN NaN

This should be my output:

name  s
a     sd 
a     fg 
a     fd 
a     wer 
a     er
b     df 
b     rf 
b     qw 
b     NaN 
b     NaN

How can I do this?

rubengavidia0x
  • 501
  • 1
  • 5
  • 18
  • Is `final result` what you want the data to look like? Also, it's good etiquette to show what you've tried so far on your own before just posting your problem and asking for someone to solve it. – Capn Jack Oct 10 '18 at 15:47
  • Yes Finaldata is the result i want – SOUMYABRATA RAKSHIT Oct 10 '18 at 15:52
  • Well then transposition is not the right term for this. I don't see any pattern in your desired result and the initial data. – Capn Jack Oct 10 '18 at 15:55
  • Your Final data is not the transposed form of Initial data. – teller.py3 Oct 10 '18 at 15:55
  • Also, if you're convinced that you do indeed want a transposition, then check out https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.transpose.html A simply google search before posting here saves everyone a lot of trouble. – Capn Jack Oct 10 '18 at 15:56
  • Just df.melt('name') – BENY Oct 10 '18 at 16:03
  • 1
    Possible duplicate of [Matrix Transpose in Python](https://stackoverflow.com/questions/4937491/matrix-transpose-in-python) – LxL Oct 10 '18 at 17:24
  • 1
    @LxL if you check the problem in body this problem is not a matrix transpose. – rubengavidia0x Nov 08 '21 at 17:23

1 Answers1

2

If you are looking to get a dataframe which looks like that, you should try to use

dataframe_name.stack()
priya
  • 46
  • 4
  • +1 more details of this method [here](https://stackoverflow.com/questions/32998893/set-column-names-when-stacking-pandas-dataframe) – rubengavidia0x Nov 08 '21 at 17:24