Stefano Pozzi
Stefano Pozzi

Reputation: 619

Issue with for loop - Pandas

I am trying to clean up a df using Pandas. I have a column containing four possible things:

  1. X.X lbs
  2. X.X pounds
  3. X.X ounces
  4. NaN

df.weight.fillna(0, inplace=True)
for i in [0, df.weight.size-1]:
    cell = df.weight[i]
    while (cell == 0 and i < df.weight.size-1):
        i += 1
        cell = df.weight[i]
    if (cell != 0):
        number = ''.join([x for x in cell if (x.isdigit() or 
        if bool(re.search('ounces', cell)):
            df.loc[i, 'weight'] = number * 0.0625    # Ounces to pounds conversion
    else:
        df.loc[i, 'weight'] = number

The aim of the code above is to iterate through the rows of 'weight' and check:

So, ideally an entry like '1.0 pounds' should become '1.0' and an entry like '1 ounces' should become '0.0625'.

What I get instead is that this code only changes the first occurrence ('1.0 lbs' -> '1.0') whilst leaving the others untouched

input:

2.1 pounds - 5.5 lbs - 8.8 ounces - 0

Output:

2.1 - 5.5 - 0.55 - 0

Upvotes: 0

Views: 56

Answers (1)

Iguananaut
Iguananaut

Reputation: 23306

In Python this

 for i in [0, df.weight.size-1]:

means a loop over the list [0, df.weight.size-1] of two items, 0, and df.weight.size-1. You probably meant something like this:

for i in range(df.weight.size)

or something similar. I suspect your code has some other problems as well but that's the first one.

By the way, if you have some transformation you'd like to apply to each item in a series, define a function that implements that transformation for a single item, and then apply it to all items using Series.apply(). In this case like df.weight = df.weight.apply(your_function). Things get a bit more complicated if the transformation depends on other values in the series, but that does not appear to be the case here.

Upvotes: 1

Related Questions