0

I have this Dataframe:

                 Value        1lag        2lag        3lag        4lag
Date                                                                     
2005-04-01  258.682029  214.382786  270.163089  253.674453  216.587332   
2005-05-01  173.253998  258.682029  214.382786  270.163089  253.674453   
2005-06-01  244.432029  173.253998  258.682029  214.382786  270.163089   

And I have this numpy.ndarray, called coef:

coef = [  1.40136101e-01   6.96820991e-02   2.95210824e-02  ]

I need to insert each of those values as a column, repeating the same value in all the lines, so it gets to look like this:

                 Value            Coef       1lag            Coef       2lag            Coef
Date                                                                     
2005-04-01  258.682029  1.40136101e-01 214.382786  6.96820991e-02 270.163089  2.95210824e-02
2005-05-01  173.253998  1.40136101e-01 258.682029  6.96820991e-02 214.382786  2.95210824e-02
2005-06-01  244.432029  1.40136101e-01 173.253998  6.96820991e-02 258.682029  2.95210824e-02

What is the best way to do it?

1 Answer 1

1

Try np.tile() to get repeat your array coef as many times as you need:

pd.DataFrame(np.tile(coef, (len(df.index), 1)), columns=['Coef']*5)

This will create a DataFrame which you can merge into your existing one.

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.