1

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...

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Andrew
  • 926
  • 2
  • 17
  • 24
  • 4
    Why are you using f-strings for this when you could just use an expression? You know an f-string outputs a string, not a float, right? – wjandrea Sep 11 '20 at 16:37

2 Answers2

2

You can use DataFrame.itertuples().

import pandas as pd 
df = pd.DataFrame([['A',3, 2000.0],
                   ['B',4, 4502.5],
                   ['C',5, 6250.0]],
                  columns=['Product', 'Number', 'Value'])
df['Unit_value'] = [tup.Value / tup.Number for tup in df.itertuples()]

print(df)

You could also use a pure python approach with zip.

import pandas as pd 
df = pd.DataFrame([['A',3, 2000.0],
                   ['B',4, 4502.5],
                   ['C',5, 6250.0]],
                  columns=['Product', 'Number', 'Value'])
df['Unit_value'] = [val / num for num, val in zip(df['Number'], df['Value'])]

print(df)

zip returns an iterable of tuples that you can then unpack into variables.

If you really want to you could do it with f strings but that would return a string and remove the ability (or at least make it more difficult) to do further calculations.

Axe319
  • 4,255
  • 3
  • 15
  • 31
  • Very nice. Much better than my way. Wanting to use `f-string` was an attempt to use this command in a slightly more complicated context than usual, just to practice with `f-string`, which I am not familiar with; but it is true that it is not necessary ... – Andrew Sep 11 '20 at 16:59
0

Firstly, don't use f-strings for this, since they output strings instead of floats. Instead, just use the plain expressions.

I'm not too familiar with Pandas, but I believe the best way to do this is by treating the columns like arrays, i.e. do the operation on the two columns themselves:

>>> df['Unit_value'] = df["Value"] / df["Number"]
>>> df
  Product  Number   Value   Unit_value
0       A       3  2000.0   666.666667
1       B       4  4502.5  1125.625000
2       C       5  6250.0  1250.000000

Otherwise, for a vanilla Python way to do it, use zip. See How to iterate through two lists in parallel?

>>> df['Unit_value'] = [x/y for x, y in zip(df["Value"], df["Number"])]
>>> df
  Product  Number   Value   Unit_value
0       A       3  2000.0   666.666667
1       B       4  4502.5  1125.625000
2       C       5  6250.0  1250.000000
wjandrea
  • 28,235
  • 9
  • 60
  • 81