0

My question is exactly same with the question

But, my language is Python, not R. So I ask this question again.

I have two time series with different time stamps and a different number of data points.

For example,

first data is

image1

second data is

image2

.

I concatenate two tables into one table.

I want to do two things. First, time index should be in order.

It is easily done by pd.concat([df1, df2], axis=1). The result is

image3

The second thing is to replace 'NA' by the most recent data point.

For example, at time 0.1, the value of column 'B' is 2.1 which is the value at time 0.09. In a same manner, the value of columns 'A' at time 0.30 should be 3.0. But still, there is no value at time 0.09 for columns 'A'.

How can I do this second job?

Thank you!

Venkatachalam
  • 16,288
  • 9
  • 49
  • 77
user155214
  • 115
  • 1
  • 5
  • Don't post pictures of your example data. Please provide a [mcve]. Refer to this post on how to do so: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – pault Jan 09 '18 at 14:58

1 Answers1

0

you can use fillna with method ffill (forward fill)

>>> df.fillna(method='ffill')
         A     B
0.09   NaN   2.1
0.10   2.0   2.1
0.22   3.0   3.3
0.30   3.0   5.1
0.33   5.0   5.1
0.50   4.0   4.0
0.59   4.0  10.0
0.60  10.0  10.0

if you want to reassign this to the same dataframe, set parameter inplace=True

oim
  • 1,141
  • 10
  • 14
  • 1
    You can also just chain it on to concat: `df_new = pd.concat([df1, df2], axis=1).ffill()` – Taylor Jan 09 '18 at 15:22