0

I'm new to Python, and I believe this is very basic question (sorry for that), but I tried to look for a solution here: Better way to add constant column to pandas data frame and here: add column with constant value to pandas dataframe and in many other places...

I have a data frame like this "toy" sample:

A    B  
10   5
20   12
50   200

and I want to add new column (C) which will be the division of the last data cells of A and B (50/200); So in my example, I'd like to get:

A    B    C
10   5    0.25 
20   12   0.25
50   200  0.25

I tried to use this code:

groupedAC ['pNr'] = groupedAC['cIndCM'][-1:]/groupedAC['nTileCM'][-1:]

but I'm getting the result only in the last cell (I believe it's a result of my code acting as a "pointer" and not as a number - but as I said, I tried to "convert" my result into a constant (even using temp variables) but with no success).

Your help will be appreciated!

Community
  • 1
  • 1
staove7
  • 560
  • 5
  • 18

1 Answers1

1

You need to index it with .iloc[-1] instead of .iloc[-1:], because the latter returns a Series and thus when assigning back to the data frame, the index needs to be matched:

df.B.iloc[-1:]                         # return a Series
#2    150
#Name: B, dtype: int64

df['C'] = df.A.iloc[-1:]/df.B.iloc[-1:] # the index has to be matched in this case, so only
                                        # the row with index = 2 gets updated   
df
#   A   B   C
#0  10  5   NaN
#1  20  12  NaN
#2  50  200 0.25

df.B.iloc[-1]                          # returns a constant
# 150

df['C'] = df.A.iloc[-1]/df.B.iloc[-1]  # there's nothing to match when assigning the 
                                       # constant to a new column, the value gets broadcasted   
df
#   A   B   C
#0  10  5   0.25
#1  20  12  0.25
#2  50  200 0.25
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • when I'm using `groupedAC['cIndCM'][-1:]/groupedAC['nTileCM'][-1:]`, I'm getting a number (0.027...). But when I'm using `groupedAC.cIndCM.iloc[-1]/groupedAC.nTileCM.iloc[-1]` I'm getting zero.. Am I doing something wrong? – staove7 Jan 22 '17 at 17:02
  • What is `groupedAC` here? Is it a grouped object? Can you show how you get `groupedAC`? – Psidom Jan 22 '17 at 17:04
  • `groupedAC = acData.groupby(by="Ntile_Cust_By_Product")["ClickIND"].agg(['size', 'sum'])` and after that I'm inserting more (new) columns which are calculated from the _basic_ data frame – staove7 Jan 22 '17 at 17:05
  • If your `groupedAC` comes from this code, then it doesn't have `cIndCM` and `nTileCM` columns, I think you mean some other data frames. – Psidom Jan 22 '17 at 17:09
  • Those columns are: `groupedAC ['nTileCM'] = groupedAC["size"].cumsum()` and `groupedAC ['cIndCM'] = groupedAC["sum"].cumsum()` – staove7 Jan 22 '17 at 17:11
  • I am not sure what could be wrong. the code seems correct to me and works on your sample data. – Psidom Jan 22 '17 at 17:17
  • Is there a way to pass it.. because, as I mentioned above, when I'm using `groupedAC['cIndCM'][-1:]/groupedAC['nTileCM'][-1:]` I'm getting the right number (but only in the last cell of the new column)... Is there an option of copying this result and insert it into a new column? – staove7 Jan 22 '17 at 17:25
  • You can extract the number with `(groupedAC['cIndCM'][-1:]/groupedAC['nTileCM'][-1:]).iloc[0]` if you like to. – Psidom Jan 22 '17 at 17:28