简体   繁体   中英

Python 'for' loop incorrectly labeling all data ranges to one label

I have a dataframe in python 3 that I called customer and an integer variable called Age, representing the age of the customer. I wanted to create custom Age bins so I used a for loop but when I run it, it labels everything as '25 to 45' - despite the fact that a glance at the Age data shows a wide range of ages. I've tried tinkering with the wording but nothing helps. Any ideas out there? Here's what I did.

First, I created the variable AgeGroup, which is a clone of Age, to replace in the loop with the label, which I do because it didn't like creating a label variable from scratch in the loop itself. Maybe this is what is tripping me up, but I couldn't find another way and I'm careful to convert to str.

customer['AgeGroup'] = str(customer.Age)

The loop is:

for a in customer['Age']:
    if a < 25:
        customer['AgeGroup'] = "Under 25"
    elif 25 <= a <= 45:
        customer['AgeGroup'] = "Between 25 and 45 (45 incuded)"
    elif 46 < a <= 55:
        customer['AgeGroup'] = "45 and 55 (55 included)"
    elif a > 55:
        customer['AgeGroup'] = "Over 55"

WHen I look at the value counts of the Age Group after the loop, they all show "25 to 45" and, puzzlingly, the dtype is listed int64. I must be missing something huge.

Any help would be really appreciated. Thanks!

I think you meant to iterate customers , not `customer:

for customer in customers.iterrows():
    a = customer['Age']
    if a < 25:
        customer['AgeGroup'] = "Under 25"
    elif 25 <= a <= 45:
        customer['AgeGroup'] = "Between 25 and 45 (45 incuded)"
    elif 46 < a <= 55:
        customer['AgeGroup'] = "45 and 55 (55 included)"
    elif a > 55:
        customer['AgeGroup'] = "Over 55"

So you're looping over just the customer['Age'] values, not the actual customer values, so you do not have access to update the customer object during the loop. Try this:

for customer in customers:
    a = customer['Age']
    if a < 25:
        customer['AgeGroup'] = "Under 25"
    elif 25 <= a <= 45:
        customer['AgeGroup'] = "Between 25 and 45 (45 incuded)"
    elif 46 < a <= 55:
        customer['AgeGroup'] = "45 and 55 (55 included)"
    elif a > 55:
        customer['AgeGroup'] = "Over 55"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM