0

I have a dataframe, df1

Items_sold        Stock
100                1.11
150                2.22
200                3.33

And another df2

Items_sold_pred    Stock_pred
50                   1.11
100                  2.22
150                  3.33

How do I add the last values of the last row of df1 to df2 column wise, such that df2 gets a final output like this? I simply add 100 and 3.33 to Items_sold_pred and Stock_pred respectively.

Items_sold_pred    Stock_pred
250                  4.44
300                  5.55
350                  6.66

2 Answers 2

2

You can use iloc[-1] to get the last row. Since your dataframes have different columns, you want to use .values to pass a numpy array:

df2.add(df1.iloc[-1].values)

Output:

   Items_sold  Stock
0       250.0   4.44
1       300.0   5.55
2       350.0   6.66

If you want to modify df2, you can use += instead:

df2 += (df1.iloc[-1].values)
Sign up to request clarification or add additional context in comments.

Comments

2

You can use tail too:

df2.add(df1.tail(1).values)

   Items_sold_pred  Stock_pred
0            250.0        4.44
1            300.0        5.55
2            350.0        6.66

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.