Skip to content
Advertisement

Divide dataframe column by a specific cell

I want to divide a dataframe column by a specific cell in the same dataframe.

I have a dataframe like this:

JavaScript

I want to compute the score_ratio by dividing the score by the ‘baseline’ score of that date.

JavaScript

The score_ratio for (date, type) = (20201101, experiment1) should be obtained by dividing its score by the score of (20201101, baseline). In this case, it should be 30 / 10 = 3. Similarly. for (20201101, experiment2), we should divide the score by the same thing, (20201101, baseline). For a different date, say (20201102, experiment1), it should be divided by the baseline of that date, (20201102, baseline).

How do I add this column with dataframe operations?

So far, I have this but am unsure of what expression I should be dividing by: df['score_ratio'] = df['score'].div(...)

Edit:

I get the error for the last line ValueError: Length of values does not match length of index

JavaScript

Advertisement

Answer

JavaScript

How it works

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement