0

I am currently learing python and I want to do the following:

I got a pandas dataframe:

Time                  A       B       ...   Z
2018-10-17 16:49:56  NaN     NaN      ...  16.28
2018-10-17 22:40:36  NaN     'String' ...  NaN
2018-10-20 01:37:32  NaN     NaN      ...  25.00
2018-10-20 11:30:39  15.0    NaN      ...  NaN
2018-10-20 12:07:04  NaN     NaN      ...  NaN

and I would like to transform it into something like this:

Time                  A       B       ...   Z
2018-10-17 16:49:56  NaN     NaN      ...  16.28
2018-10-17 22:40:36  NaN     'String' ...  16.28
2018-10-20 01:37:32  NaN     'String' ...  25.00
2018-10-20 11:30:39  15.0    'String' ...  25.00
2018-10-20 12:07:04  15.0    'String' ...  25.00

Where I always carry the value into the next row, except the row has a new item.

If I can do this, the next step would be to replace the string with a floating value which I can do with df = df.replace('string', value) and convert the Time into a numeric which contains the number of seconds t.hour * 3600 + t.minute * 60 + t.second would be my first options with the the module datetime . Currently I am unsure if this is the best way to go, because I know, there is pandas.to_datetime.

Time                  A       B       ...   Z
2018-10-17 16:49:56  NaN     NaN      ...  16.28+0
2018-10-17 22:40:36  NaN     value+0  ...  16.28+1
2018-10-20 01:37:32  NaN     value+1  ...  25.00+0
2018-10-20 11:30:39  15.0    value+2  ...  25.00+1
2018-10-20 12:07:04  15.0    value+3  ...  25.00+2

The Number of colums and rows is variable.

My Idea would be, to build a new dataframe and extract and compare the data with the old dataframe row by row, but I don't know exactly how to do it and maybe there is a more elegant way.

Can you help me?

1 Answer 1

3

You can use ffill docs

df = df.ffill()

          Time           A        B           Z
0   2018-10-17 16:49:56  NaN     NaN        16.28
1   2018-10-17 22:40:36  NaN    'String'    16.28
2   2018-10-20 01:37:32  NaN    'String'    25.00
3   2018-10-20 11:30:39  15.0   'String'    25.00
4   2018-10-20 12:07:04  15.0   'String'    25.00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.