Suppose I am working on a pandas dataframe such as the df
one produced below:
import pandas as pd
df = pd.DataFrame([['A',3, 2000.0],
['B',4, 4502.5],
['C',5, 6250.0]],
columns=['Product', 'Number', 'Value'])
df
Product Number Value
0 A 3 2000.0
1 B 4 4502.5
2 C 5 6250.0
I can use an f-string
in order to add a column, such as:
df['Unit_value'] = [f'{x/3}' for x in df["Value"]]
df
This runs fine as only 1 variable x
is involved: the denominator of x/3
is constant.
Can I do something equivalent (using f-string
), but with a variable y
in denominator, y
being the Number
corresponding to the given Value
?
What I would like to have is:
Product Number Value Unit_Value
0 A 3 2000.0 666.66
1 B 4 4502.5 1125.63
2 C 5 6250.0 1250.00
Where: 666.66=2000.0/3 , 1125.63=4502.5/4 , 1250.00=/5
I thought of something like for x in df["Value"] and y in df["Number"]
and tried to play with that, but this kind of syntax doesn't work...