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!

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

8 Comments

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?
What is groupedAC here? Is it a grouped object? Can you show how you get groupedAC?
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
If your groupedAC comes from this code, then it doesn't have cIndCM and nTileCM columns, I think you mean some other data frames.
Those columns are: groupedAC ['nTileCM'] = groupedAC["size"].cumsum() and groupedAC ['cIndCM'] = groupedAC["sum"].cumsum()
|

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.