1

I have the following dataframe:

date    A           B              C        days
1/15/19 3.373721    0.151641    0.089789    30
2/15/19 2.974086    0.1384      0.079153    30
3/15/19 2.876406    0.154186    0.076553    30
4/15/19 2.176122    0.150017    0.057916    30
5/15/19 2.099208    0.15434     0.055869    30
6/15/19 2.092377    0.15081     0.055687    30
7/15/19 2.385295    0.155956    0.063483    30

The dataframe's index is the "date" column. I am attempting to divide columns A,and B by the column named days.

I have tried the following pieces of code:

df.iloc[:,0:2]=df.iloc[:,0:2]/df['days']

I also tried this:

df.iloc[:,0:2]=df.iloc[:,0:2].div(df['days'])

Both of these operations end up resulting in NaN's in columns A and B. What am I doing wrong? Thanks

Chet
  • 421
  • 1
  • 4
  • 8
  • 1
    `df[['A', 'B']].apply(lambda x: x.div(df.days))` – Trenton McKinney Oct 20 '19 at 21:52
  • @TrentonMcKinney - thanks sir. Would you mind explaining why the code I'm writing isn't performing as expected? I've seen the same work in various other examples....just can't figure out why it's not working with my df. – Chet Oct 20 '19 at 21:54
  • 1
    `df.iloc[:,0:2]=df.iloc[:,0:2].div(df['days'])` needs `axis=0` as a parameter in `div`. So it should be `df.iloc[:,0:2]=df.iloc[:,0:2].div(df['days'], axis=0)` – Trenton McKinney Oct 20 '19 at 21:56

0 Answers0