0

I am trying to divide a column in my data set by 100 so as to turn it into a percentage (i.e. 99% = .99.) However, when I divide by 100 it returns zero for all values in the column. I understand " / " returns the floor division when dividing two integers. However, I turned the column into a float, and also divided the column by 100.0 (float.) Also, a lot of the values in the column are '100.0' to begin with, so when I divide by 100, it should return '1' if it was doing floor division. The ' + .04 ' part of the if statement is to account for an error in the report. Is it a problem with my for loop? I attached the code below.

    import pandas as pd
    import numpy as np
    
    data = pd.read_csv('Lakeshore Variance.csv')
    Percent = data['Percent at Cutoff'].astype(float)
    
    for i in Percent:
    if i < 96:
         Percent = (Percent/100.0) + .04
    else:
         Percent = (Percent/100.0)

2 Answers2

0

You can use the apply method to update a column in pandas instead of using loops.

I'm voting to close this question, as it was already answered in Python: Pandas Dataframe how to multiply entire column with a scalar

data['Percent at Cutoff'] = data['Percent at Cutoff'].apply(lambda x: x / 100)
Dharman
  • 30,962
  • 25
  • 85
  • 135
Eloims
  • 5,106
  • 4
  • 25
  • 41
0

Setup:

import pandas as pd
import numpy as np
from numpy.random import default_rng
rng = default_rng()

df = pd.DataFrame(rng.integers(100,200, (5,))) 
>>> df
     0
0  150
1  171
2  188
3  140
4  180

I understand " / " returns the floor division when dividing two integers.

No - // is the floor division operator

>>> df / 100
      0
0  1.50
1  1.71
2  1.88
3  1.40
4  1.80
>>> df // 100
   0
0  1
1  1
2  1
3  1
4  1
>>>

I turned the column into a float, and also divided the column by 100.0 (float.) ... values in the column are '100.0' to begin with, ... divide by 100, it should return '1' if it was doing floor division

Two things in that statement

  1. You are not doing floor division

    •   >>> df.astype(float) / 100  
              0
        0  1.50
        1  1.71
        2  1.88
        3  1.40
        4  1.80
        >>> df.astype(float) // 100
             0
        0  1.0
        1  1.0
        2  1.0
        3  1.0
        4  1.0
        >>>
      
  2. You expect an integer result (should return '1') from floor division between a Pandas Series of dtype float and a float. Arithmetic operations where both the operands are floats will not return an integer.

    • From Python documentation 6.1. Arithmetic conversions

      When a description of an arithmetic operator below uses the phrase “the numeric arguments are converted to a common type”, this means that the operator implementation for built-in types works as follows:

      • ..., if either argument is a floating point number, the other is converted to floating point;
        otherwise, both must be integers and no conversion is necessary.
    • 6.7. Binary arithmetic operations

      The / (division) and // (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. Division of integers yields a float, while floor division of integers results in an integer; the result is that of mathematical division with the ‘floor’ function applied to the result.

    • I'm having a hard time finding specific documentation for Numpy or Pandas. There are SO Q&A's regarding this concept but they are also alluding me.
wwii
  • 23,232
  • 7
  • 37
  • 77