1

I have two tables in python

table 1  

1   24

2   25

3   26

table 2

0   4

1   5

2   2

when I type table1/tabl2

It gives me all NaN

What I want to achieve here is getting a new table as below to show the division results

table3

1 6

2 5

3 13

Can anyone help?

Thanks

some_programmer
  • 3,268
  • 4
  • 24
  • 59
Zhen Ma
  • 25
  • 3

1 Answers1

2

You did't mention in which format your data is. But I have example of Pandas DataFrame if you use DataFrame you will get required results.

import pandas as pd

t1 = pd.DataFrame([24,25,26] , columns=['values'])
t2 = pd.DataFrame([4,5,2] , columns=['values'])

print(t1/t2)

Output

   values
0     6.0
1     5.0
2    13.0

If you want integer output please use t1//t2

  • Yes, it's two dataframe tables. Do I have to manually type out the [24,25,26] and [4,5,2]? Because my actual data has like 100+ rows in it. Say if [24,25,26] column header is sales, and [4,5,2] column header is quantity, is there a way to just put "sales/quantity"? Thanks – Zhen Ma Apr 13 '20 at 20:18
  • Yes there are several ways. Try `dataframe["sales"] / dataframe["quantity"]` – user3782095 Apr 13 '20 at 20:24
  • Thanks, it works. But just want to confirm that, in my original dataframe, table 1 has index 1-3 while table 2 has index 0-2, I have to reindex make both them 1-3 to have correct result right? Because when using dataframe["sales"] / dataframe["quantity"], python is looking for data point with same index to do the math operation, if I have 0-2 and 1-3, index 0 and index 3 only exist in one table and thus would return NaN? – Zhen Ma Apr 14 '20 at 17:20
  • Yes. so you should better re-index your dataframes. – user3782095 Apr 14 '20 at 22:29
  • And if my solution helped you so please marked it as the best answer by clicking on the green check besides my answer. – user3782095 Apr 14 '20 at 22:30