Home > Software engineering >  In Pandas how to replace dataframe column values with new values that are a function of old values
In Pandas how to replace dataframe column values with new values that are a function of old values

Time:04-04

I need your guidance on this issue i am facing, i want to some this like this:

df[x], where x is variable name that represents one of the column names. Now i want the entire column to go through this manipulation

  1. each value of column x should be process as below equation: ( 1 / (x * 22)) note that x here is a individual value of the column and it can be huge number since the huge number getting reciprocated (or 1 over x), it may result in exponential number (e /-01)
  2. The resultant number should be replacing the original number in the dataframe

If the number is 100, the new number to be put into the dataframe is (1/((100)*22)) = 4.54e-4

Please let me know how to do it rounded to 2 decimal points.

Thanks.

df[x] = df[x].apply(lambda x: (1/((x * 22)))

This is resulting in 0 in the dataframe

CodePudding user response:

df[x] = df[x].apply(lambda x: (1/((x * 22)))

looks okay, but will probably round to whole numbers. This may depend on what datatype your column x has and/or what x is.

This may work:

df[x] = df[x].apply(lambda x: (1.0/((x * 22.0)))

If your lambda function gets more complicated, for example if you want to use if-else clauses, you should write a helper function and call that inside of your apply:

def helper(x):
    if x == 100:
        return (1.0/((100)*22.0))
    else:
        return (1.0/((x)*22.0))


df[x] = df[x].apply(lambda x: helper(x))

Use round() to round the result to two decimals:

df[x] = df[x].apply(lambda x: round((1.0/((x * 22.0)),2))

CodePudding user response:

Your formula is good, but the numbers are too small to be showed in the answer (rounding with 2 decimals 0.000455 will result in 0.00).

xd = pd.DataFrame({'x':[100,101,102],'y':[1,2,3]})
 xd['x'] = xd['x'].apply(lambda x: (1/(x * 22)))
>>> xd
          x  y
0  0.000455  1
1  0.000450  2
2  0.000446  3

Try this to format the numbers to exponential format with 2 decimals.

>>> pd.set_option('display.float_format', lambda x: '%.2E' % x)
>>> xd
         x  y
0 4.55E-04  1
1 4.50E-04  2
2 4.46E-04  3

Source for formatting: https://stackoverflow.com/a/6913576/14593183

  • Related