shantanuo
shantanuo

Reputation: 32316

Transpose columns with repeated data

I have the data stored in columns that I need to change to rows. The transpose method is not working as expected.

             reg_no            st_name six  seven  eight  nine  ten
number
1        1200210606      DORIN    18      28      98     78    58
2        1001200049      PRAMA    79      69      59     19    29
3        1205210026      PILLA    47      57      67     27    17
4        1205210064      SUSAT    16      16      66     76    86
5       10002100113     PAVITH    15      85      75     65    15

The expected results will look something like this.

1   1200210606  DORIN   six 18
1   1200210606  DORIN   seven   28
1   1200210606  DORIN   eight   98
1   1200210606  DORIN   nine    78
1   1200210606  DORIN   ten 58
2   1001200049  PRAMA   six 79
2   1001200049  PRAMA   seven   69
2   1001200049  PRAMA   eight   59
2   1001200049  PRAMA   nine    19
2   1001200049  PRAMA   ten 29
3   1205210026  PILLA   six 47
3   1205210026  PILLA   seven   57
3   1205210026  PILLA   eight   67
3   1205210026  PILLA   nine    27
3   1205210026  PILLA   ten 17
4   1205210064  SUSAT   six 16
4   1205210064  SUSAT   seven   16
4   1205210064  SUSAT   eight   66
4   1205210064  SUSAT   nine    76
4   1205210064  SUSAT   ten 86
5   10002100113 PAVITH  six 15
5   10002100113 PAVITH  seven   85
5   10002100113 PAVITH  eight   75
5   10002100113 PAVITH  nine    65
5   10002100113 PAVITH  ten 15

Upvotes: 0

Views: 106

Answers (1)

Chitrasen
Chitrasen

Reputation: 1726

You are trying to convert wide format to long format. Use melt function for that.

ref : http://pandas.pydata.org/pandas-docs/stable/generated/pandas.melt.html

import pandas as pd

pd.melt(df,['number,reg_no','st_name']) # df is your dataframe object

you can use sort() after you melt to get the exact order

Upvotes: 3

Related Questions