1
$\begingroup$

Sample input dataset is:

enter image description here

Sample output dataset what i want:

0   2017-11-17 10:23:28.691 788
0   2017-11-17 10:23:28.691 756
0   2017-11-17 10:23:28.691 789
0   2017-11-17 10:23:28.691 780
1   2017-11-17 10:23:29.731 788
1   2017-11-17 10:23:29.731 783
2   2017-11-17 10:23:30.655 747
3   2017-11-17 10:23:31.627 766
4   2017-11-17 10:23:32.606 807

How can I do this by pandas? or is there any other technique to do this?

$\endgroup$
3
  • 1
    $\begingroup$ This is probably best suited for StackOverflow I think? It's a purely programming question. $\endgroup$
    – Mephy
    Nov 22, 2017 at 13:56
  • $\begingroup$ I think this is fine. It's a data wrangling question. $\endgroup$
    – Louis T
    Nov 22, 2017 at 20:57
  • $\begingroup$ Thanks, everyone. I posted to Stackoverflow and got the answer stackoverflow.com/questions/47437966/… $\endgroup$
    – An0mn
    Nov 23, 2017 at 9:26

1 Answer 1

2
$\begingroup$

Assuming you have the following source DF:

In [21]: df
Out[21]:
         Time  val1  val2  val3
0  2017-11-17    11    12    13
1  2017-11-18    24    25     0
2  2017-11-19    37     0     0

Solution:

In [22]: (df.replace(0, np.nan)
            .set_index('Time', append=True)
            .stack()
            .reset_index()
            .rename(columns={0:'val'})
            .drop('level_2',1))
Out[22]:
   level_0        Time   val
0        0  2017-11-17  11.0
1        0  2017-11-17  12.0
2        0  2017-11-17  13.0
3        1  2017-11-18  24.0
4        1  2017-11-18  25.0
5        2  2017-11-19  37.0
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.