4

I have a dataframe similar like this,

cat_A  cat_B  cat_C  cat_D  dog_A  dog_B  dog_C  dog_D
  3      2      4      1      9      8      10     6
 ...
 ...

I knew how to calculate between columns by using column names, like

df['ratio_A'] = df['cat_A']/df['dog_A']

cat_A  cat_B  cat_C  cat_D  dog_A  dog_B  dog_C  dog_D  ratio_A
  3      2      4      1      9      8      10     6      3/9

But when I tried to generate multiple columns by calculate each of those columns, are there any other easier ways to calculate all columns and append new columns by once? Instead of

df['ratio_B'] = df['cat_B']/df['dog_B']

df['ratio_C'] = df['cat_C']/df['dog_C']

df['ratio_D'] = df['cat_D']/df['dog_D']

When the column length become very large it will be a lot of lengthy code to copy and paste. Do I need to create 2 lists like,

l1 = [cat_A, cat_B, cat_C, cat_D], l2= [dog_A, dog_B, dog_C, dog_D]

Then using for loops to implement?

1 Answer 1

5

IMO a good practice here would be to work with MultiIndexes instead of flat columns:

df.columns = pd.MultiIndex.from_tuples(map(tuple, df.columns.str.split('_')))
df
  cat          dog          
    A  B  C  D   A  B   C  D
0   3  2  4  1   9  8  10  6

At this point, computing the ratio is very simple courtesy index alignment.

df['cat'] / df['dog']
          A     B    C         D
0  0.333333  0.25  0.4  0.166667

res =  df['cat'] / df['dog']
res.columns = pd.MultiIndex.from_product([['ratio'], res.columns])

pd.concat([df, res], axis=1)
  cat          dog               ratio                     
    A  B  C  D   A  B   C  D         A     B    C         D
0   3  2  4  1   9  8  10  6  0.333333  0.25  0.4  0.166667
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.