MrSoLoDoLo
MrSoLoDoLo

Reputation: 375

Adding values to a dataframe columnwise

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

Upvotes: 0

Views: 357

Answers (2)

anky
anky

Reputation: 75080

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

Upvotes: 2

Quang Hoang
Quang Hoang

Reputation: 150735

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)

Upvotes: 2

Related Questions