0

I'm a bit new to panda and have some diabetic data that i would like to reorder.

enter image description here

I'd like to copy the data from column 'wakeup' through '23:00:00', and put this data vertical under each other so I would get a new dataframe column:

5.6  
8.1  
9.9   
6.3  
4.1  
13.3  
NAN  
3.9  
3.3  
6.8  
.....etc
busybear
  • 10,194
  • 1
  • 25
  • 42
user613326
  • 2,140
  • 9
  • 34
  • 63
  • I think `.melt`, to preserve `NaN`. There are examples in https://stackoverflow.com/questions/34376053/pandas-dataframe-stack-multiple-column-values-into-single-column – ALollz Feb 14 '19 at 22:31
  • Any NAN should stay as NAN here. – user613326 Feb 14 '19 at 22:33

2 Answers2

0

I'm assuming the data is in a dataframe already. You can index the columns you want and then use melt as suggested. Without any parameters, melt will 'stack' all your data into one column of a new dataframe. There's another column created to identify the original column names, but you can drop that if needed.

df.loc[:, 'wakeup':'23:00:00'].melt()
    variable  value
0     wakeup    5.6
1     wakeup    8.1
2     wakeup    9.9
3     wakeup    6.3
4     wakeup    4.1
5     wakeup   13.3
6     wakeup    NAN
7   09:30:00    3.9
8   09:30:00    3.3
9   09:30:00    6.8
...

You mention you want this as another column, but there's no way to sensibly add it into your existing dataframe. The shape likely won't match also.

busybear
  • 10,194
  • 1
  • 25
  • 42
  • your right on that it should indeed be in a new dataset, note though that in your list the wakeups are all below each, its not what i want. – user613326 Feb 15 '19 at 17:14
0

Solved it myself finally took me quite some time. Notice here the orginal data was in df1 result in dfAllMeasurements

dfAllMeasurements = df1.loc[:, 'weekday':'23:00:00']
temp = dfAllMeasurements.set_index('weekday','ID').stack(dropna=False) #dropna = keeping NAN 
dfAllMeasurements = temp.reset_index(drop=False, level=0).reset_index()
user613326
  • 2,140
  • 9
  • 34
  • 63