1

I have pandas dataframe with float columns X and Y and the string column A.
The content of float columns are positive float numbers.

I am extracting the row where  column A='a' and another row where column A = 'b'
and I am doing division of float columns of 2 rows (a/b) as in code below.
The problem is that result of division is NaN. Why?
I tried to use axis=0 and axis=1 but result is the same.

I am using python pandas version 1.0.5 Here is my code:

import pandas as pd    ddd = {    'A': ['a', 'b'],    'X': [100.0, 20.0],    'Y': [6.0, 2.0] }    df = pd.DataFrame(ddd)    df1 = df[df['A'] =='a']    df2 = df[df['A'] == 'b']    print(df1)    print(df2)`    res = df1.iloc[:, 1:].div(df2.iloc[:, 1:], axis=0) print(res)    res = df1.iloc[:, 1:].div(df2.iloc[:, 1:], axis=1) print(res) 

Result

 X Y 0 NaN NaN 0 NaN NaN 

2 Answers 2

1

Don't use boolean indexing. Rather set "A" as index:

ddd = { 'A': ['a', 'b', 'c', 'd'], 'X': [100.0, 20.0, 5.0, 2.0], 'Y': [6.0, 2.0, 1.0, 1.0] } df = pd.DataFrame(ddd).set_index('A') lst = [('a', 'b'), ('c', 'd')] out = [df.loc[x]/df.loc[y] for x, y in lst] 

Output:

[X 5.0 Y 3.0 dtype: float64, X 2.5 Y 1.0 dtype: float64] 

Or, index all combinations at once:

df = pd.DataFrame(ddd).set_index('A') lst = [('a', 'b'), ('c', 'd')] x, y = map(list, zip(*lst)) out = (df.loc[x].div(df.loc[y].values) .set_axis(lst) ) 

Output:

 X Y (a, b) 5.0 3.0 (c, d) 2.5 1.0 

df:

 X Y A a 100.0 6.0 b 20.0 2.0 c 5.0 1.0 d 2.0 1.0 

NB. I'm assuming letters in "A" are unique.

If you have missing values,reindex:

ddd = { 'A': ['a', 'b', 'c'], 'X': [100.0, 20.0, 5.0], 'Y': [6.0, 2.0, 1.0] } df = pd.DataFrame(ddd).set_index('A') lst = [('a', 'b'), ('c', 'd')] x, y = map(list, zip(*lst)) out = (df.reindex(x).div(df.reindex(y).values) .set_axis(lst) ) X Y (a, b) 5.0 3.0 (c, d) NaN NaN 
Sign up to request clarification or add additional context in comments.

Comments

0

.div will only work on the two rows if the indices are the same so you need to use:

df2 = df[df['A'] == 'b'].reset_index(drop=True) 

which then will gives for res:

 X Y 0 5.0 3.0 

1 Comment

This answer works. However, when I added 2 more rows to dataframe with col(A) = 'c' and col(A)='d' and I tried to call code above in the loop over pairs of values in llst = [('a','b'), ('c','d')] where every element in every tuple is matching column A , 1st element nominator and 2nd is denominator I have found that only 1st division for tuple (a.b) works as expected, for the second tuple ('c','d') it returns row with (NaN , NaN )

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.