If I understand you correctly, you are describing the following:

Setup

d = {'id1': ['x', 'x'], 'id2': ['z','w'], 'metric': [100,10] }
df = pd.DataFrame(data=d)
df

Solution

# Manually choose the value by which to scale the column 'metric'
scaler = df.loc[(df['id1'] == 'x') & (df['id2'] == 'z'), 'metric'].values

# Divide all 'metric' values by the above scaler value
df['result'] = df['metric'] / scaler

df
  id1 id2  metric  result
0   x   z     100     1.0
1   x   w      10     0.1
Answer from Peter Leimbigler on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › divide-a-dataframe-in-a-ratio
Divide a DataFrame in a ratio | GeeksforGeeks
August 17, 2020 - Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.Pandas dataframe.floordiv() function is used for integer division of the dataframe with
Discussions

Pandas - Creating new column with proportion/ratio from criteria/other cols
Similar to the other comment but I'd suggest just using a groupby apply to get a summary df.groupby('Surgeon').apply(lambda x: sum((x['Preop'] == 1) &(df['Conversion'] ==1))/sum((x['Preop'] == 1))).reset_index(name='Conversion Rate') More on reddit.com
🌐 r/learnpython
3
2
January 26, 2024
python - Calculating the ratio of two columns - Stack Overflow
Dataframe with four columns: x1 x2 x3 x4 Desired output: x1/x2 x1/x2 x1/x3 x2/x3 x2/x4 x3/x4 I want to create new columns which are ratios of the original columns. Only way I could th... More on stackoverflow.com
🌐 stackoverflow.com
python - How to calculate ratio of values in a pandas dataframe column? - Stack Overflow
I'm new to pandas and decided to learn it by playing around with some data I pulled from my favorite game's API. I have a dataframe with two columns "playerId" and "winner" lik... More on stackoverflow.com
🌐 stackoverflow.com
pandas - Adding new column in dataframe by taking ratio of two existing columns - Stack Overflow
I want to create a new column in dataframe by taking a ratio of two existing columns. Following code works but it does not retain the column df[price_per_sqft]. df['price_per_sqft'] = (df['SalePr... More on stackoverflow.com
🌐 stackoverflow.com
🌐
w3resource
w3resource.com › python-exercises › pandas › python-pandas-data-frame-exercise-38.php
Pandas: Divide a DataFrame in a given ratio - w3resource
part_30 = df.drop(part_70.index): ... the rows from df. ... Write a Pandas program to split a DataFrame into two parts in a 70:30 ratio and then output the row counts for each part....
🌐
Quora
quora.com › In-DataFrame-how-should-I-compute-between-rows
In DataFrame, how should I compute between rows? - Quora
Answer (1 of 4): Use shift(). i.e. [code]df['Cl'] - df['Cl'].shift(1) [/code]pandas.Series.shift - pandas 0.22.0 documentation
🌐
Reddit
reddit.com › r/learnpython › pandas - creating new column with proportion/ratio from criteria/other cols
r/learnpython on Reddit: Pandas - Creating new column with proportion/ratio from criteria/other cols
January 26, 2024 -

Hi guys,

Fairly new to python - i'll try to be clear as I can

I have a dummy DF with some columns of appointments in healthcare - Surgeon, Conversion (1/0 or Yes/No)

I want to create a new column with "Conversion Rate" i.e. what % of the Surgeon's appointments resulted in a Conversion (1/True)

In plain english that looks like - if surgeon matches the one highlighted, return sum (Conversion) / count (Surgeon) but i'm struggling to get that delivered + applied via a function.

Here's my rough stab below but any thoughts/advice appreciated!

I am using pandas to do this and I do have other columns so I need to create a new column only with this rather than any over-arching DF operations. So if the surgeons name appeared 17 times I want the conversion rate column to show the same rate for that surgeon each time.

def convrate(surgeon):

for surgeon in df['Surgeon']:

ConvPts = 0

Non-Conv== 0

if df['Preop'] == 1:

if df['Conversion'] == 1:

ConvPts += 1

else:

Non-Conv += 1

return ConvPts / (ConvPts + Non-Conv)

🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.pct_change.html
pandas.DataFrame.pct_change — pandas 3.0.1 documentation
Computes the fractional change from the immediately previous row by default. This is useful in comparing the fraction of change in a time series of elements. ... Despite the name of this method, it calculates fractional change (also known as per unit change or relative change) and not percentage ...
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › divide-a-dataframe-in-a-ratio
Divide a DataFrame in a ratio
November 2, 2023 - Following example divides the dataframe in a ratio of 70% and 30% using the numpy.split() function. import pandas as pd import numpy as np dic = {"Letters":['A','B','C','D','E','F','G','H'], "Number":[1,2,3,4,5,6,7,8]} data = pd.DataFrame(dic) print("The Original data:") print(data) print("The 70% of the original data") train_data, test_data= np.split(data,[int(0.7*len(data))]) print(train_data) print("Another 30% of the data") print(test_data)
🌐
Stack Overflow
stackoverflow.com › questions › 54752750 › calculate-ratio-with-pandas › 54752998
python - calculate ratio with pandas - Stack Overflow
Use groupby and just calculate the ratio of sum over size using transform to broadcast the results to original size.
🌐
Sololearn
sololearn.com › en › Discuss › 2748074 › i-cannot-figure-out-what-do-the-ask-me-to-output
I cannot figure out what do the ask me to output | Sololearn: Learn to code for FREE!
Then output the dataframe row where the ratio is equal to the max() for the dataframe. It only takes a couple lines of code. df['newColumnName'] = ratio_formula print(df[df['newColumnName'] == df['newColumnName'].max()]) ... This is my code: ###### This is the given code ####### import pandas as pd df = pd.read_csv("/usercode/files/ca-covid.csv") df.drop('state', axis=1, inplace=True) df.set_index('date', inplace=True) ############################# # Looking for max value serie = df.describe() max = serie.loc['max'] ratio = {'cases':max['cases'], 'deaths':max['deaths']} df['ratio'] = ratio['cases'] - ratio['deaths'] dt = df.loc['26.12.20'] df_dict = { 'cases':dt['cases'], 'deaths':dt['deaths'], 'ratio':dt['ratio']} dt = pd.DataFrame(df_dict, index = ['26.12.20']) print(dt)
🌐
Stack Overflow
stackoverflow.com › questions › 72159338 › pandas-calculating-ratio-between-values-of-dataset-for-some-subset
python - Pandas: calculating ratio between values of dataset for some subset - Stack Overflow
May 8, 2022 - I have a dataset which looks like value 34 45 3 -3 I want to calculate ratio of values for this dataset, i.e. ratio of value to next value: 34/45 , 45/3, 3/-3 I can do it via myDataset["value&...
🌐
CopyProgramming
copyprogramming.com › howto › pandas-groupby-and-aggregate-two-columns-for-respective-totals-then-calculate-ratio
Python DataFrame GroupBy Two Columns and Count: A Complete Guide for 2026
December 7, 2025 - This guide covers the complete methodology for grouping by two columns, counting combinations, aggregating multiple columns for distinct totals, and calculating ratios—all while implementing the latest pandas 3.0 best practices and performance optimizations for 2026. Grouping by two columns and counting the occurrences of each unique combination is straightforward using the groupby() and size() methods. The size() method counts all rows in each group, including those with null values, making it ideal for counting group combinations.
🌐
Stack Overflow
stackoverflow.com › questions › 63427536 › pandas-taking-ratio-of-difference-from-the-row-above-and-store-the-value-in-anot
python - Pandas taking ratio of difference from the row above and store the value in another column, with multi-index - Stack Overflow
August 15, 2020 - #df # A B C # total diff total diff total diff #0 100 0 200 0 20 0 #df2 # A B C # total diff total diff total diff #0 200.0 NaN 50.0 NaN 30.0 NaN # Take last row of current DataFrame i.e. `df` curr = df.iloc[-1].xs('total', level=1) #Get total values # Take total values of new DataFrame you get everyday i.e.
🌐
Saturn Cloud
saturncloud.io › blog › how-to-calculate-ratio-of-values-in-a-pandas-dataframe-column-a-comprehensive-guide
How to Calculate Ratio of Values in a Pandas DataFrame Column: A Comprehensive Guide | Saturn Cloud Blog
August 25, 2023 - To calculate the ratio of values in a column, we’ll use the pct_change() function, which computes the percentage change between the current and a prior element. This function by default calculates the percentage change from the immediately ...