4

Lets say I have a data frame like this

ID,Time1,Value1,Time2,Value2,Time3,Value3
1,2,1.1,3,1.2,4,1.3
1,5,2.1,6,2.2,7,2.3

And the expected dataframe is this

ID,Time,Value
1,2,1.1
1,3,1.2
1,4,1.3
1,5,2.1
1,6,2.2
1,7,2.3

If the row has unique id, the pd.wide_to_long works perfectly in such case.

df = pd.wide_to_long(df, ['Time',Value],'ID','value', sep='', suffix='.+')\
    .reset_index()\
    .sort_values(['ID', 'Time'])\
    .drop('value', axis=1)\
    .dropna(how='any')

but how to fix in such situation, if row's ID is not unique

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
user96564
  • 1,578
  • 5
  • 24
  • 42

1 Answers1

7

Trick is use reset_index for column of unique values:

df = (pd.wide_to_long(df.reset_index(), ['Time','Value'],i='index',j='value')
        .reset_index(drop=True)
        .sort_values(['ID', 'Time'])
        .dropna(how='any')
        )
print (df)
   ID  Time  Value
0   1     2    1.1
2   1     3    1.2
4   1     4    1.3
1   1     5    2.1
3   1     6    2.2
5   1     7    2.3

Detail:

print (pd.wide_to_long(df.reset_index(), ['Time','Value'],i='index',j='value'))
             ID  Time  Value
index value                 
0     1       1     2    1.1
1     1       1     5    2.1
0     2       1     3    1.2
1     2       1     6    2.2
0     3       1     4    1.3
1     3       1     7    2.3
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252