0

enter image description here

I trying to achieve the same with this line of code:

df['Math/Physic']=df.groupby(['Rank'],sort=False).apply(lambda x:x['Maths']/x['Physics'])--->doesn't work

I am getting this below error:

TypeError: incompatible index of inserted column with frame index

This code below work though. It looks like , the code above not able to fetch value from those columns:

df['Math/Physic']=df.groupby(['Rank'],sort=False).apply(lambda x:10/2])--->works fine

I have tried different variations but not able to get this working. Any help on this will be highly appreciated.

I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • 1
    please provide your input as reproducible code or text (not images!) – mozway Jul 25 '22 at 12:55
  • Try this : `df['Math/Physic']=df.groupby(['Rank'],sort=False).apply(lambda x:x['Maths']/x['Physics'], axis=1)` – I'mahdi Jul 25 '22 at 13:02
  • I am getting TypeError: () got an unexpected keyword argument 'axis'. Tried out a solution from :https://stackoverflow.com/questions/47227280/pandas-groupby-apply-on-multiple-columns-to-generate-a-new-column df_new=df.assign(Math/Physic=df.groupby('Rank',group_keys=False).apply(lambda x:x['Maths']/x['Physics']))--->work now but had to assign the result to new dataFrame. new column show up on df_new.head() or df_new.columns – Prasanta Saha Jul 25 '22 at 13:57
  • 1
    Please provide enough code so others can better understand or reproduce the problem. – Community Jul 25 '22 at 23:18

0 Answers0